当前位置: 动力学知识库 > 问答 > 编程问答 >

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

I imported the Framework at the top of my project

But I still get this error:

Use of unresolved identifier 'JSON'

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.

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

网友答案:

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!

网友答案:

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.

网友答案:

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.

网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: