Adding Playgrounds to your Xcode Project

Logan Wright
6 min readOct 1, 2015

Since the addition of Swift, one of my favorite new features has been the Playground. These are great for rapid iteration on an idea until we get it just right. Unfortunately, sometimes we want to use features of our existing code base within these playgrounds so that we can access and build on features we’ve already created. Today, we’re going to talk about how you can use your project’s code base inside of a playground.

Create a New Project

First off, let’s create a new single view project, specify our language to be Swift, and call it HelloPlayground. The specific settings don’t matter too much, but here’s a shot of mine just in case. If you’re working with an existing project, just skip to the next step.

Note: Your project should have a target 8.0+, and Swift.

New project settings

Add Code To Share

Now let’s create a file that we will eventually access from our playground. Let’s name it Hello.swift. Since this is just an example, we’re only going to create one method. Your file should look like this:

func hello() -> String {    return “Hello Playground”}

Make it a Workspace

We’re going to leave that file for now. This next part is easy, just go to File -> Save As Workspace.

You should save this in the same directory as your .xcproject file, and name it the same thing. These steps may not be absolutely necessary, but they will make your life easier.

Add Playground To Workspace

Now that we have our workspace, let’s add a playground to it. The easiest way to do this is to add a new file with `cmd + n`, then under source select Playground. I personally like to name mine `Playground`, but you can name yours whatever you want.

Create Framework Target

We’re not quite ready to use our playground yet. For our playground to be able to access the code from our project, we’ll have to add a new target to our project that builds to a framework. First, select your Xcode project in the workspace, then create a framework target by going to File -> New -> Target.

Next, select iOS, then select Cocoa Touch Framework from the Framework & Library section.

Now, we’ll name our target. I like to name my targets by the project name, followed by the OS type as a suffix. You can name yours however you prefer. Don’t check the Include Unit Tests box unless you have a specific reason to do so. For our purposes, we do not.

Name Our Framework

If you try to build your framework, you may get an error. This is because of the hyphen included in the name. We’re going to rename it to match our project anyways, so let’s do that now. Select your project in the navigator, then select HelloPlayground-iOS -> Build Settings. Once in the build settings menu for our application, type Product Name into the search bar, then enter HelloPlayground into the name field.

Add Files To Framework

Next we need to make sure that the files we want to access are added to our framework target. Let’s add Hello.swift to our target.

Any new files added to our project will need to be added to the framework target if we want to access them.

Build Our Framework

Now we can finally build our framework. Select your framework, and iOS Device from the build sources. Once this has been selected, press the play button, or build with cmd + b.

Note: Sometimes updated code wasn’t getting updated to my playground. Building to iPhone 6 Simulator, and then to iOS Device again helped resolve this.

Import Our Framework

Now, we can finally import our framework into our playground. At the top of your playground file, add:

import HelloPlayground

Now, we’ll run the hello() function we declared earlier. Oh NO! ERRORS!

Our original function, hello() doesn’t specify access control explicitly, so it defaults to internal which means we can’t access it outside of the module. There’s two ways we can fix this problem. The first way to mark that function explicitly public. This way isn’t very safe if we don’t want this function to be public in our general project, but we want to be able to access it here. Instead, we can use the new @testable keyword provided in Xcode 7 to import our framework and use it as if we were internal.

Adding Functionality

Remember that whenever we add new features that we want accessible from our playground, we’ll have to rebuild our framework.

Going Forward

I have been using this as a great way to quickly iterate new features I’m adding without having to constantly rebuild the project. These features can then be copied into the project and quickly integrated with confidence.

Clean Up

If you’re like me, and like to keep a clean project, you can clear out quite a bit of the default files added by the framework. I’m not sure if this will have other implications, but it won’t affect your playgrounds usage. First delete the entire HelloPlayground-iOS folder, including the .h, and the Info.plist.

Now we’ve removed the Info.plist file, so we’re going to specify to just use our the plist already existing in our project. Do this by navigating to the framework’s settings page, selecting Choose Info.plist File… and selecting your project’s plist.

Growing Pains

I realize that this is a lot of work, but if you’re on a project for a long time, it could save you more time in the long run. It’s important to remember that these tools are still new, and they haven’t quite reached maturity yet. Hopefully Apple will continue to iterate and build new features onto playgrounds to make uses like this even easier in the future.

:)

More

If you’re interested, follow me on twitter, or see what some of my colleagues and I are up to at Intrepid Pursuits.

--

--