There’s been some great changes to playgrounds in Xcode 10 that will make your code even easier to prototype than ever before. Let’s take a look at some of the changes.
In Xcode 10 open a new single view playground, and save it as Xcode10Playground. If you are familiar with the Xcode 9 playground single view template, you’ll notice a difference. Instead of setting up my view in viewDidLoad
, this sets up my view in loadView
. This will prevent a bug that happens in iPad playgrounds and sometime Xcode where the superview is still nil
in viewDidLoad
.
When working with view controllers and setting up your user interface, use this pattern of adding all your views to a superview, then assigning the superview to the view property of the view controller. Also make sure you set the frame of the superview, which you can get from the nativeBounds
property of UIScreen
. nativeBounds
will be in portrait. Since I use iPad playgrounds most of the time, this code in my view controller sets the frame correctly for iPad landscape, and adds a label, which I’ll have to layout and set properties elsewhere. Strangely it doesn’t always work, But I’m still trying to find a consistent solution. This works more often than others.
Xcode Dark Mode
A big feature change to macOS is dark mode, and Xcode supports this. I can show you the really cool features a little easier in dark mode. So I’ll go to Editor, select Theme and select Presentation Dark.
If you look to the right of the line numbers, you’ll see some lines.
That’s for code folding, which is a new feature to Xcode Playgrounds. If you hover over the line you get a set of arrows giving you an idea what you are about to fold. I can fold
Loadview and MyViewController by clicking on the fold line.
I’ll click on the arrow and they unfold again. If you don’t have code folding or want to turn it off. There is a setting in Xcode preferences. Go to text editing and check on or off Code folding ribbon according to your preference.
Selective Run Mode
You’ll also notice that circle with a play button next to playground page on the line numbers. The biggest change is a selective run mode.
I can still run using the run on the bottom toolbar, but you can also run using this button. I can click on the new play button, open the live view and my playground runs.
I’ll stop the playground and close the live view. Notice what happens when I drag the play button up and down the code. It skips over the class.
This will only let you run code that makes sense. Nested statements or classes, essentially anything you can fold, will not run.
Fold up the view controller and give your self some space above it to play with this. Add a variable a with a random number using my new favorite API. iOS 12 has a simple to use random function, which you use as as class method of the type your are working with.
var a = Int.random(in: 1...10)</pre>
Then make a simple for loop from one to five. I'll skip a variable for it with a underscore.
for _ in 1...5{ }
There a beautiful new random number generator in iOS 12. random(in:)
is a class function you give a range to, working with several types of numbers. I’ll add a random number of 1 to 10 to the sum of a
:
a += Int.random(in: 1...10)
Then print this
print(a)
I’ll then move my mouse over the line numbers, and you can see that assignment, the print, and the end of the for loop can run. I’ll run the assignment and I get a random number on the right.
The line number is no longer highlighted.
Run at the end of the loop and you get five iterations of the summation, which you can see if I open up a viewpoint.
I also have more lines not highlighted, telling me I ran those already. Change the assignment line by adding 1 to it, and the run resets. with a blue number
I’ll run to the print this time. I get my result in the panel and the code runs. Note the random number is frozen so you can step through this code. That’s different than pressing the run button on the bottom which changes your random numbers. If you are testing with data that might change every execution, selective execution make life much easier.
While it does not run within a class, this selective run can help you prototype your code, letting you change and run as you step through an algorithm. Write the code like I did here and then make it a function or class. You can trace and build your code easily when you need to see every statement run.
Leave a Reply