Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use SwiftyJSON in my iOS App?

I recently added SwiftyJSON into my xCode project using Carthage.

Cartfile

$ cat Cartfile
github "SwiftyJSON/SwiftyJSON" >= 2.1.2

I ran this command to install SwiftyJSON

$ carthage update

I added the built Framework to my xCode project

enter image description here

I imported the Framework at the top of my project

enter image description here

But I still get this error:

Use of unresolved identifier 'JSON'

enter image description here

Seriously, what am I doing wrong? I'm new to iOS development.

UPDATE: I tried removing the Frameworks and re-dragging the frameworks to the Linked Frameworks and Libraries and Adding the "Run Script" section to "Build Phases" as the instructions in Carthage asked.

enter image description here

I'm convinced that something went awry during the Carthage Update process because I received this error on my initial attempt:

$ carthage update
*** Fetching SwiftyJSON
*** Fetching Alamofire
*** Checking out SwiftyJSON at "2.1.3"
*** Checking out Alamofire at "1.1.4"
*** xcodebuild output can be found in /var/folders/0x/swzt630n3_575tglljpwhn4h0000gn/T/carthage-xcodebuild.Pc6rLg.log
*** Building scheme "Alamofire iOS" in Alamofire.xcworkspace
*** Building scheme "Alamofire OSX" in Alamofire.xcworkspace
*** Building scheme "SwiftyJSON" in SwiftyJSON.xcworkspace
** BUILD FAILED **


The following build commands failed:
    Check dependencies
(1 failure)

After looking up the problem, I was told that you could run

carthage update --configuration Debug

Which installed it fine, but something is still amiss...

like image 381
andrew nguyen Avatar asked Mar 25 '15 05:03

andrew nguyen


People also ask

Does JSON work with IOS?

JSON is a popular data-interchange format used by APIs (application programming interfaces). JSON lets you bundle a large amount of data into one chunk of text and then send it along to another service.

How do I add SwiftyJSON to Xcode?

Adding SwiftyJSON to the demo project Here's our path: Xcode > ( Xcode project name) > Targets > (Xcode project name). In the General tab, under the Frameworks and Libraries dropdown, we click on + and select Add package dependency. Then, we enter the package Git URL: https://github.com/SwiftyJSON/SwiftyJSON.git.


2 Answers

It seems like you missed a step during the addition of SwiftyJSON and Alamofire frameworks from Carthage. Check out these steps from the Carthage readme page :

Steps :

  • Create a Cartfile that lists the frameworks you’d like to use in your project.
  • Run carthage update. This will fetch dependencies into a Carthage/Checkouts folder, then build each one.
  • On your application targets’ “General” settings tab, in the “Linked Frameworks and Libraries” section, drag and drop each framework you want to use from the Carthage/Build folder on disk.
  • On your application targets’ “Build Phases” settings tab, click the “+” icon and choose “New Run Script Phase”. Create a Run Script with the following contents:

    /usr/local/bin/carthage copy-frameworks
    

    and add the paths to the frameworks you want to use under “Input Files”, e.g.:

    $(SRCROOT)/Carthage/Build/iOS/Alamofire.framework
    $(SRCROOT)/Carthage/Build/iOS/SwiftyJSON.framework
    

Looks like you missed step 4. I tried these steps myself in a demo Swift project and they are working great. Try these steps out, and if you still have problems, leave a comment. Thanks!

like image 146
aksh1t Avatar answered Oct 16 '22 00:10

aksh1t


I switched over to Cocoapods and it just worked. Again, I'm a total noob when it comes to iOS development. When looking for a dependency manger I looked at Cocoapods and Carthage for what they had to offer. I was intrigued by Carthage's claim of keeping things simple:

"Carthage because we wanted the simplest tool possible—a dependency manager that gets the job done without taking over the responsibility of Xcode"

While I always will choose the simpler tool when given a choice, I think I don't know enough of the iOS ecosystem (read: Xcode) to use Carthage.

like image 42
andrew nguyen Avatar answered Oct 15 '22 22:10

andrew nguyen