Using Alamofire within a Playground

2019-01-23 15:29发布

问题:

I'm new to iOS development and using Xcode and I'm having trouble getting Alamofire to work within a Playground. There's some functionality I'd like to test out for proof of concept but the library is not linked to the Playground and I've tried getting it to play nicely. I have Alamofire set up to work within an iOS (not in a Playground) project before the installation instructions in the Github Alamofire repo were recently updated.

Any suggestions on how to get Alamofire to import properly in the Playground?

回答1:

Apple provides excellent step-by-step instruction to do so here: Importing Custom Frameworks Into a Playground



回答2:

Here is what I consider to be the simplest straight line path:

Download and build Alamofire:

brew install carthage
echo "github \"Alamofire/Alamofire\"" > Cartfile
carthage update

Configure the workspace with a playground file and Alamofire.

  1. From the Carthage/Build/{iOS / OSX} directory, drag the Alamofire.framework file into your link library list: {Project Name} -> {Target Name} -> Build Phases -> Link Binary With Libraries
  2. Command-N (create file) -> {Platform} -> Source -> Playground
  3. If not already in a Workspace file, File -> Save as Workspace. To be paranoid, I close the project / workspace and (re)open the workspace.
  4. Click on the playground file created in #2, and add the import import Alamofire


回答3:

You can create a playground within the project also like in this tutorial

Steps

  • If the project is not in a workspace, save as workspace
  • Create new Playground, by adding to workspace level in the project pane
  • Ensure your project has a framework target. If it doesn't Edit the scheme to add a new Cocoa Touch Framework target (it doesn't need to have unit tests)
    • Add the files that you want to use in the playground, to the new framework target
  • Build the Framework by selecting the target in the build box at the top
  • In the playground, import the Framework. If the classes you want to use are not public, you need to import like so: @testable import ModuleFramework


回答4:

You can try moving library to

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk/System/Library/Frameworks/

Then you can import and use it right in your playground