7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

1.4 More About Playgrounds

For the remainder of this course we will be exploring the Swift language through the use of Playgrounds within Xcode. In this lesson we will go into a little more detail on what Playgrounds are and some of their more useful features.

1.4 More About Playgrounds

Throughout this course, we're going to be using the playground feature of Xcode rather exclusively. So it definitely pays have a little bit of an understanding as to some of the features and functionality that you can find within playgrounds that are proved to be extremely useful. Now, first of all, to rehash a little bit about what the playground consists of, we kind of have two main channels here. We have where your code goes here, which has kind of been the center off to the left. Where you're gonna type in any sort of code and it's going to be read and evaluated as kind of part of the REPL loop that we started talking about before. And then anything that has to be evaluated that produces some sort of result can be printed over here on the right. So as you can see here by default, when you create a playground, you have these two lines. You are importing your UI kit, if you chose to model this after the iOS SDK during the process of creating the playground. And then we're given this kind of initialization and creation of a string variable. Now there's couple things to note here. Now when we write this import UI kit line, we get nothing on the right, since there's nothing really happening, there's nothing being evaluated. There's nothing to print. There's nothing to show. But the second line is a little bit different for two reasons. First of all, we're using the var keyword. Now, this is a feature where a functionality of the Swift programming language that allows us to use something called type inference. Now this is a very cool feature that's been around in a couple other languages for a little while. And I really enjoy using this because it definitely saves me some keystrokes throughout the life of my program. But at the same time some others don't really care for it so you can kind of pick and choose what you'd like to do. But what the var keyword tells the Swift programming language and ultimately the compiler, is that I'm not going to explicitly tell you what this particular string or what the type of this particular variable is. I want you to figure out that on your own based on the initialization that I'm going to provide. So as you can see here I'm creating a variable called str and I'm initializing it with, Hello playground. Now this is within double quotes. So, this is obvious, obviously a string to you and I. But to the compiler and to the Swift programming language, it's able to infer that from this point going forward, whenever I see the str variable, I know that it's going to be a string. Now, the second part of interest, here, is that, because this actually does do some work and does provide some sort of evaluation and execution. We're going to get some printed out value to the right, here, to show what has actually happened, and we have assigned the Hello, playground string to the str variable. So now at any point from here on out, if I were to type str, now obviously there's nothing going on there and this wouldn't actually compile within your code but I'm able to just peek into the value of the str variable at this given time. Which is very, in a very important concept as we go because what you're going to find out when we're working with these playgrounds, is that everything is going to be executed from the top down every time you modify this playground. So at this particular time, str is the same as it was up here. Now, if I were to come down after this initialization step, and say, all right, I now want to append onto this. So we'll use the plus equals syntax and I want to append to this, the exclamation point. So now you'll see something very interesting has happened. The original step where we have initialized our str variable has stayed as, Hello playground, without the exclamation point. But since I have modified that and have appended an exclamation point to the str variable in the middle before the step where I just printed it out. Now both of them have exclamation points and that's because I have interjected this line in between that has now subsequently changed the value of the str variable. Everywhere else within this code from the top down. So, once again, i can remove this and now it's going to change back to the original value. So that's something definitely to keep in mind as you're modifying these playgrounds. You can just kind of change things willy nilly all over the place, but you're going to see that it's going to have effects later on in your code down further in the playground, if you start playing around with certain variables as you go. So, let's look at another very simple example of where you can start to use some of the tools that you're going to find within these playgrounds. So, let's just say I'm starting to work with some numbers and I create a var a and I'm gonna initialize it to say, 100. Now once again, you're gonna see here that this actually evaluates and prints out to the value 100. But what if I wanted to do something a little bit more interesting instead of just that, instead of just adding to this? Maybe I wanted to do some sort of looping functionality where I add some numbers together or continue to multiply some numbers together or something along those lines. Well, I can obviously do that with a loop. So I'm gonna create a simple for loop, then I'm going to say, for the variable i, and I want to give this value of i a range of values over time, say from one to ten. So I can say in 1 and I can use the range operator, which is basically the ellipses or three periods, and I can say, for the values 1 to 10. Now I can go ahead and do something with that. So I can maybe say, a is now going to be plus equal to i. And you'll see, once I've exited from that loop, we're now able to look over to the right, and you'll see something that you maybe didn't expect. I'm now getting the number of times that this particular loop was run. So I can maybe change this to 100, if I wanted to and I can jump out. Now you can see, it's been executed 100 times. Well that's all fine and dandy, but what if I wanted to look into the value of a and wanted to find out exactly what the value was? Well if I were to switch this back to 10 so the view becomes a little bit easier to understand. Let's transition our view a little bit over to the right where things begin to print out. Now this is more than just maybe a console or a debug view where you can just start to print random values, that it's much more intelligent than that. So if you were to hover over any of these values you're going to see that I have two little icons over the right. I have a little eyeball here that is actually going to provide a quick look. So if I click on this it's going to say 100 times, okay nothing too interesting there. And if I come over to Hello, playground it's gonna do the same thing. So this is going to allow us to see more data than maybe could fit within this little channel for each individual line. So if I had a lot more data here than this Hello, playground, I could choose to take this into the quick look and maybe be able to see more data. So what about this loop scenario? What does this really mean? Well, if I come over here and I see this little plus button, you are going to see this is the value history. Now if I were to click on this, we're going to start to see something quite interesting actually. We're going to see here, a nice little graph that's actually produced just out of the box for no other reason than the fact that this is the default view that's given when you start to look at a for loop within what's called the Assistant Editor on the right over here. So I got to this by clicking on this little plus icon here to see the value history. So if I were also to come up here, I could look at View. I can look at my Assistant Editor and I can say where I want this to show up. I could have them show up on the right, on the bottom, or I can have them stacked horizontally or vertically. But it's more than just a pretty little chart. Now let's say I wanted to come in here and I actually wanted to see some of this data. So, what I could do is I could use one of the built in global functions that the Swift programming language has that we'll talk a little bit later when we dig into the standard library. And that's going to be println. Another reason why I'm sticking with the playgrounds throughout this course is because of this right here. This, this smart code completer IntelliSense that's going to show me the options that I have when it comes to actually printing out some data. So I could come in here to my println function and I could start to put in some data in here. So let's say I wanted to maybe print out the value of a. Well, as you can see here now, I now get this cute little graph that's showing me how my value of a is increasing. But now I also get a console output because I used println a. And I can see all the different values for all the ten times that this was executed. So, I could get rid of any sort of questions I might have around what these values are, by simply doing things like printing things out, or things like that. So I can see this assistant editor on the right, or wherever I chose to dock it. I can look at the values within this for loop. So that's a very handy little feature. Now, one other thing you're going to see throughout this course that when we start to type in some information or type in some keywords or use some classes or some methods that we don't quite understand, it's very helpful to have quick access to documentation. And documentation really took a front seat when it came to Xcode 6, which is very exciting, especially if you've ever had to dig around in some of Apple's documentation on their website. So one thing that you'll be able to notice, if you hold down the Cmd button, I can start to hover over different things within my code to see what they are or to see what sort of access I have to their documentation. So as you can see here UI Kit is lit up as well as the println function. So, I can click on that and it's gonna open up and take me to the definition of that particular function, so I can see maybe what its inputs or outputs are. So I can also jump back here, I can also go into View and I can open up Utilities and say, I want to show my Quick Help Inspector. And that's gonna open up another view on the far right hand side, so I can start to click on things. And once I click on them, say, for instance, the println function, I'm going to get some Quick Help on the right that's going to describe what this function does as well as the declaration. So I can start to learn more about what these things are within my playground as far as maybe classes or functions or other things like that. So I can learn more about them as I go and see their definitions and their usage. And all of these things put together throughout this course are gonna prove to be very fruitful and very useful you, for you as you learn more about the Swift Programming Language.

Back to the top