Next lesson playing in 5 seconds

  • Overview
  • Transcript

1.3 The REPL

Throughout this course we will not be creating actual applications using the Swift language. Instead we will be exploring the different features and constructs of the language. In previous versions of Xcode this would have been a nightmare of creating countless test applications and deploying to devices and/or simulators. With the advent of Xcode 6 we now have access to a REPL (Read, Evaluate, Print, Loop). It's OK if you don't have experience with REPLs in the past. We are going to cover your options in this lesson.

1.3 The REPL

If you've ever done any sort of development with languages such as Python or Ruby, then you're probably familiar with the concept of a REPL. Now if you're not, a REPL, or REPL in itself is just an acronym that stands for Read, Evaluate Print Loop. Now a REPL is an interactive environment that allows you to write statements or commands or access methods or classes or things like that interactively. So that you can see within your REPL session what's happening when you write certain lines of code. What happens if you call this class? How do you call this class? How do you use this method or things of that nature. Now previously before Xcode 6, you had to go and jump through an awful lot of hoops just to be able to investigate or play around with new APIs or different features of the Objective C Language. You would have to create a brand new project. You'd have to fill out some information about that project. You'd have to write a full piece of code that actually would fully compile into an iOS application. That you would then have to deploy into either the simulator or to a physical device and then actually run the application and see what's going on. Now, this is a very lengthy process that can be kind of a pain after a while. Especially if you're trying to use or get access to some of the more complicated features or APIs within the iOS STK. But with the advent of Swift, and actually, more importantly, Xcode 6, we're now provided with a REPL that's going to allow us to interactively work with this development environment. So we can play around with things, like the swift programming language. Now there's a couple of different ways that you can get at this particular REPL. One is through the command line, and one is through Xcode. So I'm going to fire up iTerm, and as you can see here, I have just a blank canvas here currently. Now to start up the swift REPL. What we're going to do is we're going to type in xc run. Which is an Xcode command. And then I'm going to pass to it Swift. And what this is going to do is going to bring up the Swift REPL. As you can see here, you can type :help to get a list of the help topics that you can use to learn more about this environment. But let's just kinda blast into a little bit of code here, nothing too complicated but just so you can see how this is going to work. So let's go ahead and say I want to create a new instance of a string variable. So I'm gonna call this Var Hello, and I'm going to initialize it to Hello, World. This is fairly simple and probably something you've seen 100,000 times before. So go ahead and hit Enter, and as you can see here, I've used the keyword var. Now, var is going to tell the compiler that this hello variable is going to be implicitly typed, and you need to determine what the type is in itself. Which is very cool, and I love those types of features. That's going to do things for me cuz I love saving keystrokes whenever I can. So, now it knows that the hello variable is in fact a string and has been initialized to Hello World. Now, at any point in time I could type in hello and it's going to spit out the value of what it finds as well as the type. So let's say I wanted to do something with hello. Well, let's say I wanted now to. I wanted to append onto the end of that, so I'll say hello is going to be equal to hello plus, and I want to append an exclamation point. So now, if I type in hello, I'm going to get Hello World with an exclamation point. Pretty cool stuff. Now this is nothing mind blowing and I'm not doing anything too crazy here. But the power is in the concept of this REPL. I don't have to write a fully building piece of code that's going to ultimately have to output something on the simulator or on the device just to see what's going on. I can do it all interactively right here within this session. And I can do that with any sort of class that accessible via the iOS SDK or any sort of construct therein. And I can play with just about anything. So, this is very cool and I definitely like playing with this for very quick things on the command line to be able to do this via XC run. Now if you want to exit out of here, you go ahead and just type the colon quit and it will go back to your typical shell. Now you can also do this via Xcode. So let's fire up ex code, now remember, this has to be Xcode 6. And I'll go ahead and click this first option at the top here and get started with a playground. So I'm going to name this MyPlayground. I'll just call it MyPlayground2. And you pick your platform, which is ultimately going to choose which of the SDKs you're going to have access to. It's not really going to matter too much for us but we're going to select iOS because that's. Kind of what we're getting to throughout this course and then we'll click Next. And then, it'll say where do you want to save that? And I'll just say, I'll save it on my desktop, and there we go. Now, you can see here that I have two panes. I have this pane on the left which is right I get to write my code. I have this little channel in here on the far left, which is going to become very interesting here shortly. And I kinda had this execution here on the right. So as you can see I have a variable STR that's initialized to hello playground. And I'm giving a value or an execution value here of hello playground. Now we also have a line up above here called import UIKit. Now we're not gonna use that very much. But you could import different libraries through that or either part of the iOS SDK or other third parties or whatever you have by simply specifying this import keyword. So that is a pretty cool feature as well. So let's do something very similar to what we did in the command line REPL. So I'll say, var Hello. And as you can see, on the left-hand side as I'm typing these things, I got a little error here before because it knows that something's not going right here. So it's saying here that I have an Unterminated string literal. So the compiler is running constantly through here to double check the things that I'm doing, to make sure that I'm doing them correctly. So we are going to initialize this to Hello, world and it is immediately executed and we get the value of Hello world here on the right. Now we can also play around with this string whenever we want. I can type out just string hello here. And as you noticed as I was typing that out, I'm getting full intellisense and code complete here. So, this is one of the main reasons that I like to play with the playgrounds as opposed to the command line version of the REPL, because I get full intellisense. I get everything, all those wonderful features of Xcode while I'm playing around in my playground. In code that is probably not going to go anywhere and probably wouldn't compile once I screw around with all of this stuff. So as you can see here, I can just type in hello and then I get the particular value stored therein. So now let's say I want to say all right, hello is now going to be equal to hello and I want to append an exclamation point. So there you go, so at the point in time where I had written this statement or these two statements here. Hello had a value of just Hello world. But now that I have appended to it the exclamation point, every time I type hello from this point on, it's going to have an exclamation point, which is very cool. So this is very time sensitive and the order in which you do things is where things are going to be stored. So that's about all I want to get into at this point. We're gonna start digging into more features of the playground as we go on into other lessons as we need to. But this is very cool, and we're gonna be playing around in these playgrounds throughout the rest of this course. So it's def, definitely something you're gonna want to get used to using.

Back to the top