Review the changes in Xcode 10 playgrounds.
- [Instructor] 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 those changes. Now notice I'm using version 10 beta 4 for this, so expect a few more changes coming by the time you see this. In Xcode 10, open a new single playground, so I'm gonna go here to get started with a playground, and I'm gonna use the single view to show you something, and we hit next, and I'm gonna go to the desktop, and I'm gonna make this my Xcode 10 playground.
I'm gonna create that, and you got a playground. I'm gonna get the whole screen here, so you can see everything, and you'll notice something interesting right off the top if you've used Xcode 9 playgrounds for single view, that we are putting all of this code here into load view, not view did load. This solves an error that you can get if you put it into view did load that the self.view here is nil, and so what this is all doing is getting that self.view to not be nil.
The code from here is just filler. It's only these two lines that are of importance for this. So what we're gonna do here is look at a few new changes though, and the big feature change is dark mode in Mac OS, and Xcode is gonna support this. I can show you most of the really cool features a little easier in dark mode because of the contrast. So I can get the dark mode here by going to the editor menu, going to theme, and going down to presentation dark.
And that'll give us a little bit more contrast for what I'm going to do. Now the next thing I'm gonna do is that there's gonna be a new ribbon right here for code folding. Now mine's not up yet, so I'm gonna go into preferences, and go over to text editing, and make sure that the code folding ribbon is turned on, and I can close this up again. And now you'll see that we got these little bars here. And what that allows you to do is fold your code up. So you don't have to see it, I can fold all my code into one line if I need to.
I can open it back up again, and everything inside of it will open up. So that's one change. The other change is down here, you can see this little play button. There's the regular play button over here which still works. You can, for example, execute this playground. And you get the hello world here. If I change this to green, it disappears, and I can execute it again. This time, however, I'm gonna execute it using this little circle that you can see moving around here.
You'll notice sometimes it has blue lines and sometimes it has gray lines. I want it as a blue line, I'm gonna go all the way down here to the bottom here, click it, and it runs just like before. Now this is a way of running code line by line. I'm gonna get rid of the view here. I'm gonna fold up my view controller, so I have more space to play around with this. I'm gonna give myself some room, and I'm gonna add some code here. Now there's a new function that I really like in iOS 12 as well, and I'm gonna use that to start this var, a = Int, and here's this great new function, random, which gets you easy random numbers.
All you need to do is put in 1...10, you just put the range in, and you get yourself a random number. I'm gonna make a simple for loop from one to five for this, so I'm gonna do for, I'm gonna do underscore 'cause I don't need that, in 1...5, and that gives me a loop, and inside there I'm gonna add another random number.
Random in:1...10, like so, and then underneath all that I'll just print(a) like so. So that's a little bit of code. You'll see that you got your code folding again, you can fold up your for loop, anything will be able to do that, but now I'm gonna take this little arrow thing, and you'll see what happens. I can play here, but in the middle of the loop, anywhere probably where you're gonna find code folding, you can't run the code.
It'll only step through code as you go through, but I can go to line six here, I can hit this and I get my answer. I can then drag this down and say, "Okay, at the end of the for loop I can run that." So I can go ahead and run from here, and I can see what happens there. I can pop this open and see my code. I can do the same thing for the a, and get what my actual value is, and it's 38. Now the nice thing about all this is this is running just this much code, and it leave these things alone, which is why I used a random number.
You may be having situations where you're using random numbers. You may be having user input, and you wanna watch what happens to that user input as you make changes. So I can actually make another change here. I'm gonna make a change right here, and add +1. You'll notice the ribbon goes blue again because it's now ready to restart that code again. I can come back down to line 10 and run this and see what happens.
I can also stop the code over here, and I'm gonna show you one more thing here. So I can run line six and leave that alone, and then as I go through, I can stepwise go through my code. So I can do the line nine, see what happens there, and fix things as I go along. While this doesn't run within a class, as you can see with this class view controller, I can't do anything down here, the selective run can help you prototype your code, letting you change in run as you step through an algorithm.
Write the code like I did up 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.