Using 3rd party frameworks in Xcode Playgrounds

Last updated: December 29, 2019. Original version: Oct 10, 2018

apple developer xcode swift swift 5 ios swift ios 13 tutorials tutorial guide free xcode playgrounds playgrounds framework library third party 3rd party teaching

Xcode Playgrounds is a great place to prototype code and even practice Test-Driven Development. And sometimes, 3rd party libraries might be handy to use. In this article, I’m gonna show you two ways to use Xcode Playgrounds with frameworks.

The problem we’re trying to solve

3rd party frameworks can’t simply be included in a playground, not even as part of "Sources":

3rd party library import error

I. The Simple Way (for when using a single framework)

Say we have a framework that we want to test in a playground — for the example below, I’ve picked a well known library, Alamofire.

Alamofire - Elegant HTTP Networking in Swift

The downloaded archive contains Xcode project that can be opened directly. Make sure the project builds (⌘+B) successfully:

Alamofire - Elegant HTTP Networking in Swift

The Playground

Let’s now create the playground that’s we're going to use. With the project still opened, from the menu, select File > New > Playground, make sure it’s an iOS playground, and select the 'Single View' template.

New Xcode Playground

You can use the default name (MyPlayground.playground) and store it inside the framework itself - make sure to select Alamofire under "Add to" and "Group":

New Xcode Playground

Once the playground is created, run it to view its output:

Empty Xcode Playground

Using the framework is pretty easy now:

1. Import it at the top:

import Alamofire

2. Add a viewDidLoad method and make an HTTP request:

override func viewDidLoad() {
    super.viewDidLoad
    AF.request("https://httpbin.org/get").response { response in
        debugPrint(response)
    }
  }

And here is the result:

HTTP call output

II. Using Multiple Frameworks

Let’s see how we can use multiple frameworks in a playground - we’re going to use the same Alamofire together with SwiftyJSON, "the better way to deal with JSON data in Swift".

To start with, download both libraries and store them side by side in a folder:

Using two libraries

Now, we'll open the Alamofire Xcode project as before and we’re going to create a workspace around it — from the menu select: File > Save as Workspace. I’ve named mine: Playground.workspace and saved it beside the two folders:

Workspace

Adding the Second Framework

Let’s now add the second framework — from the SwiftyJSON-master folder, drag and drop SwiftyJSON.xcodeproj onto the workspace beside the Alamofire project.

The workspace should now contain the schemes from both frameworks.

Schemes

The Playground

Just as we did in the previous section, we’ll create a playground inside the workspace— from the menu select File > New > Playground, make sure it’s an iOS playground, and select the ‘Single View’ template. Make sure it is added to the Playground workspace and the Playground group:

New Xcode Playground

One Scheme to Rule Them All

Now, before we can use the frameworks, we’ll have to build them one by one. Whilst it’s not that hard when two are included, it can be a nuisance when using a bigger number of libraries or if you actively develop them whilst using with a playground. For this, we’ll add a new scheme that will build all frameworks.

From the Scheme picker, select "New Scheme...":

New Scheme

Make sure the target is “None” — I’ve named mine Playground:

New Scheme

On the next screen, under the Build category, use the little + button to add the two frameworks as dependent targets — here is how it should be configured:

New Scheme

Click “Close”. Now, every time you build the Playground target, all the dependents will be also built.

NB: Make sure you’re building for a Simulator and not a real device.

Using the Frameworks

The frameworks can now be used inside the playground just as before:

1. Import them

import Alamofire
  import SwiftyJSON
  

2. We're going to load a sample JSON from https://httpbin.org/json and parse the slideshow title property:

override func viewDidLoad() {
    super.viewDidLoad
    AF.request("https://httpbin.org/json").responseData { response in
      guard let data = response.data else {
          debugPrint("Error retrieving JSON")
          return
      }
      guard let json = try? JSON(data: data)else {
          debugPrint("Invalid JSON")
          return
      }
      let title = json["slideshow"]["title"].string
      debugPrint("Title: \(title ?? "-")")
    }
  }
  

When the playground is run, you’ll now see the title displayed the console:

HTTP call output

That’s it!

Happy Playgrounding! 🤓