How do I use SwiftyJSON in my iOS App?
Asked Answered
G

4

10

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...

Gabbert answered 25/3, 2015 at 5:10 Comment(2)
Can you check where the SwiftJSON files are in your project directory?Nereen
In Carthage/build/iOSGabbert
Q
2

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!

Quixote answered 25/3, 2015 at 6:33 Comment(1)
I tried redoing what you suggested, but the problem still persists... Check out my updated question.Gabbert
G
1

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.

Gabbert answered 25/3, 2015 at 15:36 Comment(0)
N
0

TL,DR: Can't use it inside of Emulator, Use iPhone.

I also followed your way, and I found out that --configuration debug will code-sign "iOS Developer" identity as SwiftyJSON's project property.

AFAIK, Code-signed "iOS Developer" identity wasn't run with Emulator. My issue was resolved when I run it at my iPhone 6.

Nicko answered 11/4, 2015 at 8:31 Comment(0)
K
0

I do not know why you can not add this resource with help of Carthage but if you need use SwiftyJSON you only need:

  1. Enter this URL: SwiftyJSON

  2. Clic the button "Clone or download" and then in "Download ZIP" like the next image shows:

Clic the button "Clone or download" and then in "Download ZIP" IMG

  1. You will get a ZIP file with name "SwiftyJSON-master"

  2. Extract the files

  3. Enter the path:

SwiftyJSON-master/Source/

  1. You will see a file with name:

SwiftyJSON.swift

  1. Copy that file into the files of your project.

  2. Add that file into your project directory.

  3. That file will be like other class into your project that you may use.

Kayo answered 30/8, 2016 at 18:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.