How To Test A UI Code (iOS, Swift) — Part 1
By: Hamoon Jamshidi Meydandar
Tags: UI, iOS, Swift, Ui Testing, iOS App Development

Then I remembered…

*Drum rolls*

Swift Playground. Where you can play with your code and see the result, right there.

The glorious Swift Playground! The promised heaven of Swift developers!😍

I remembered that we can test any Swift code in the playground, and I remembered that I saw bunch of developers posting about how they use Playground to see how their UI code looks like in action!

The simplest approach for starting, is just creating a Single View and copy paste your code there and watch the beauty!

On the left, your code. On the right, the result.

But…

My app has grown bigger than Hello World, I need the views to interact with many parts of my app. It is tedious to copy past everything there! And what about my Cocoapods?? Even when I have the Playground inside my project, I couldn’t access other files from my Playground. So, I searched around the web for a solution, and I found this:

Step 1: Adding the Playground to your project

Create a Playground, go and find it in Finder drag and drop it to your Workspace and not the project in Xcode! You should be able to see it among your files in your Workspace. Even if you create it in the Workspace’s folder(I think it’s actually a good idea to do that) it won’t be inside your project. So, you need to drag and drop it, go to Files-> add files to "<your project name>"

You can see your Playground in your Workspace after adding it.

Step 2: building a framework from your project

It’s quite easy. Go to File > New > Target and choose "Cocoa touch framework". Important: choose Embed in Application to None. Give it a name. I chose "NiceminFramework".

Now, go to each file that you want to play with, in the playground, right click and from the File Inspector go to Target Membership, and choose the framework. Leave your app included.

It is important to set any class, method or anything to public, if you want to have access to them.

You can also set them to Open. Public means you can read them, test them outside of the declared framework, but you can't change them. Open means you can ALSO change them.

Now, build the framework. Go to upper left side of the Xcode, click on your App name, choose your framework. AND choose a simulator for target. If you choose something else for the target, it won’t work in the playground! And now build it with cmd⌘ + B. And every time you made a change in your code, you should re-build the framework, if you want the change to reflect on your playground.

Step 3: Let the fun begin

Choose your playground, on the top write import <your framework's name>. If there is no error it means you can use the files that you choose to be included in your framework (from each file's file inspector) and you set them to open.

Here, I made a ViewController and I want to see how does it look like. I make the MyViewController a subclass of the View Controller I want to test.

Then run it by clicking on the start► button on the left.

Also you want to choose assistant editor on top right (two circles)

and then click on manual (top middle) and from the menu choose live view