Grab your Shovel and Pail, There’s a Better Kind of Playground in Xcode
Testing code and functionality is a vital part of building any application. Unfortunately though, testing during the build phase can sometimes be difficult and / or time consuming. Typically, my approach to do some quick testing and troubleshooting while writing code was to instantiate objects in the App Delegate file under the following function.
I would then put print statements either in the class’s functions or in the App Delegate. Once the program launched, this function executed creating instances of each object, and then I would review what printed to the console to evaluate if the desired functionality was achieved. This however, was time consuming, and to be blunt, annoying.
Another approach I would take was to copy my files over to a playground and run tests there. This was convenient because it would show progress and information along the right side. It offered a bit more than the app delegate in terms of functionality and testing. This was inconvenient though because once the application gets larger than a few files, you’d have to copy quite a lot to copy. Additionally Playground files typically compile and function in the order they are written and a line of code is not aware of anything that is written below it. Therefore this introduced some issues once the program gets even slightly intricate.
Knowing that there is a way to create playground files directly inside of an Xcode project I thought there must be a way to make everything in the project available in that playground, and sure enough there is! Feel free to open up Xcode and follow along as I’ll walk through every step and share some code along the way.
First, let’s create and name a new project. Once that’s complete right click anywhere in the left hand file structure and click New File just as you would if you were to add a new swift file. From here choose Playground as seen below.
Let’s create a couple more files so we can do some testing as well. See below for examples used.
Now that we’ve created some test files let’s head over to our playground and try to create a person. You’ll notice that none of the initializers are available in autocomplete and you’ll receive an error like the below.
Just because the playground exists inside the Xcode project doesn’t make everything from the project available to it. We will have to create a framework in the project and then import that framework into the playground. To create a framework click File -> New -> Target. Choose Cocoa Touch Framework from the menu and click next.
Name the framework and click finish. Leave all other options as is. Prior to clicking finish your screen should look like the below.
Now that a framework is created we must add files from the project to it so they can be accessed when the framework is imported. If you click on the Person file you’ll notice on the file inspector that the framework created is now available as a Target Membership
Place a check in the box next to the framework. Do this for Gender and Dog as well. This will include the contents of the Person, Gender, and Dog files in the framework. Now let’s jump over to the playground file. This framework will need to be imported in just like you would any other framework. In order to do that you’ll need to place “@testable import FrameworkName” at the top of the playground file. All items you’ve included in the framework will now be available to you in the playground. Let’s try and test a few things.
Success! Classes from the project are now available in the playground. Once changes are made to any item that is included in the framework those changes will not be reflected in the playground until the code is compiled. In general, ‘Clean’ and ‘Build’ are your friends when it comes to frameworks, so if anything is not working properly, that should be the first step to troubleshoot.
Additionally, when adding new files to your project you can add them to the framework at the time of file creation by choosing the appropriate targets. Let’s add a car class to the project. By placing a checkmark next to the DemoKit target, the car class will be added to the framework immediately.
Now let’s add some code to the car class, and then build and see if it’s available in the playground file. Also let’s update the Person class to include the variable car of type ‘Car?’.
Since we checked the box at the time of file creation there was no need to add it later. Items can be added and removed from the framework at any point in time. Additionally, multiple frameworks can be created so you can pick and choose what to import into the playground file.
Now go have fun testing code in your new and improved fully loaded playground files