1.3 Development Options
You have several options for environments that you can use to follow along with this course. You could use the REPL found at the command line. You could also use Playgrounds, which are simply GUI wrappers around the REPL. What I recommend, however, is to create Swift projects in Xcode. I'll talk through these options in this lesson and show you how to create a Swift 2 project in Xcode.
1.Introduction3 lessons, 15:05
2.Foundational Concepts5 lessons, 43:51
3.Working With Collections3 lessons, 26:26
4.Controlling Flow7 lessons, 1:22:24
5.Functions5 lessons, 51:54
6.Creating Types6 lessons, 1:08:10
7.Conclusion1 lesson, 01:15
1.3 Development Options
When it comes to playing around with the Swift language, specifically Swift 2.0, and in the previous versions as well, you have three main options for starting to use the language and getting familiar with with it. Two of them are very similar as in one is actually just a wrapper around the other and then the third is a little bit more advanced. So the first one, if you really are just new just new to Swift and you want to see what it feels like and just kind of throw some Statements out there and use some built-in functions. Then you can absolutely use what is known as the Swift interpreter, which is really just a repple. So all we're gonna do is we're gonna open up a Terminal. And here we have the option, and I'm assuming at this point that you have installed Xcode 7. At this point we have two different options. We can open up in our favorite text editor, any file, and we can start to write Swift code, and then we can save it as a .SWIFT file. And then pass it to the Swift interpreter, which is just Swift. We could say Swift, and then I could pass in a filename here, and it would execute that Swift code so you can use a simple text editor to write yourself some code and then execute it through Swift. Or you could simply type in swift at the command line and hit Enter. And it's gonna enter you into the interactive shell. So at this point I can just start to type statements like I said into a REPL, R-E-P-L, and if you've ever worked with any sort of other interpreted languages, a REPL shouldn't be anything new. But if you haven't, a REPL simply stands for read, evaluate, print, loop, which is going to allow you to just write some code out here, write some statements. And it's going to continue executing these or evaluating these and then printing out the results and then looping around and continuing to do that. So this is very good for, like I said, just getting to start with the language and playing around with some of the functionality. And I can do pretty much anything I'd like in here, I can create variables, I can save our name, it's going to be equal to Derek. And it's going to tell me what that particular variable is and what type it is. And as you can see here I didn't specify that it is a string, but it figured that out on its own. And that's one of the nice features of Swift is that it's able to infer the types of things based on the assignment statements that you work with. And then I can also query and say all right, what is the value of string at this point? So if it were to modify and change over time. I can go and see what it's changed to. I can call basic functions like print. I can print out name and get the result like that. And I can even import libraries or frameworks. At this point I could say import foundation, and then I'm going to get access to all the different types and functionality that come along with foundation. Things like NSURL and some more of the advanced types that are built into not only Objective C, but also into Swift. And even if they're not built into Swift you're able to bridge from Swift into Objective C even if those different types and functions and different functionality aren't necessarily natively written in Swift. So that's pretty nice. One of the downsides to using the repel, or the in the interpreter, is that it's slow and some of the debugging capabilities, or actually, the lack of debunking capabilities, is one of its downsides. So if you want to use this to play around and feel out some of the features of Swift and just see how it works, then this is absolutely, not a bad place to start. So you can definitely get a feel for how to start to write the code. So you can use this to follow along for a while and I don't mind if you do that but at some point you're going to need to branch over into something else. Now, the something else, there's two other ways that you can start to work with Swift. And actually the next version is something called a playground. And a playground is nothing more than a UI wrapper around this interpreter or around the repel. So if I want to get out of this I can just use Ctrl+D, and I can clear this out and be done with that. Now if I want to use Playgrounds, I'm going to need to open up Xcode. And as you see here the first option I have here is get started with a playground, so I can go ahead and click on that, and I'm going to need to give this a name, and we'll just leave it as MyPlayground. And you can choose a platform here, iOS or OS X. Probably for the most part throughout this course I'm going to select OS X simply because it's going to give me the most trimmed down pieces of functionality when it comes to not only the playground, but also when we start building projects using Xcode. So it's just probably easier for me while I'm showing you so I don't kind of clutter up the screen and my project with a whole bunch of extra folders and libraries that I don't need, but you can absolutely choose the iOS platform. In replace of OS X, it's not going to matter that much. So I'll go ahead and hit Next. And then I need to save it someplace. So I'm going to save it to my desktop. And then I'll go ahead and hit Create. So as you can see I have myplayground.playground, here on my desktop. And I've been given a little bit of boilerplate here, so as you see I have a comment here at the top. It's imported the cocoa library, and I've created a string variable here. And if we wanted to kind of follow along similarly to what we had done before in the [INAUDIBLE] in the interpreter in the command line. I created a variable called name and I set it equal to Derek. Which is not a big deal, and as you see eventually, it takes a moment, but it will show up on the right hand side. So every time you write lines of code down the left, in the right gutter here you're going to get the result of running that code. So at this point, I could say, all right Name and I could concatenate onto that, Jensen and then you'll see on the right hand side that we're going to get my full name Derek Jensen. And same thing with the playgrounds as with the interpreter because behind the scenes, behind the playground is actually the interpreter. It's just a wrapper around that REPL. So it's going to be a little bit slower you might get a little bit more in the way of debugging help or information about errors as they come up. So let's say I wanted to print my name. And I say prin, and I forgot the T, just a simple error and then I say print name and something happens. A number of things happen, but more specifically two that are very visible. The first thing is that on the right hand side you can see that everything has gone gray. And typically that's a sign that something bad has happened. Either you have an error in your code or something bad happened behind the scenes or maybe the interpreter crashed or something happened out there. Now typically that's going to happen, maybe a little bit less than you making mistakes in your playground, but it's a possibility that that could happen also. So I can also come over here to the left, and you're gonna see this little red exclamation point. And I can click on this and it's gonna give me some information about what the problem that the interpreter has found it says that it has found an unresolved identifier print. So I could come back in here and change that to print, and it's going to work just fine. As we're going to get the print over on the right hand side as you can see here. That print, by default, is going to add in the slash and or the new line. So those are some of the basic characteristics and things that you can do within the playground. You can do the same things that you can do within the REPL or in the interpreter because ultimately, like I said, it's just a wrapper around it. Now, once again you can follow along this course primarily using just the playground cuz you can write full applications here and really be able to execute them and see the different pieces, as you come down along the way. But some of the shortcomings are, once again, it is slow because the REPL is constantly running in the background and it takes a moment or two for it to actually show up and give you the result. And you may not get as full featured information about any sort of errors or problems that you have introduced into your program. So this will work once again to play around with the language a little bit and get a feel for it. But ultimately the third option is probably going to be your best and the one that I'm going to recommend the most, and that's simply by creating an application. So to do that we're going to close out our playground. And I'm going to come back to Xcode, and I'm going to say, I want to create a new Xcode project. Now at this point, I can once again create just about anything I want, any sort of type, any sort of template I want to use. But once again I'm going to come down to the OS X application and simply create a command line tool, simply because it's going to give me kind of the most trimmed down version of a project. So there's not going to be a whole lot of extra stuff that you're going to have to sift through while you're learning how to program but you can use pretty much any of these other options you can create an iOS application or framework or library. WatchOS, OSX application whatever you really want is gonna be okay. So I'm gonna choose to create a command line tool. Follow along with me if you'd like or you can create something else, but there may be a few deviations here and there. So I'll go ahead and select Next, and once again I'm going to have to give this a name, so I'll just call the sample. And then I will make sure at the bottom that I was select the language that I want to use, and remember, we're talking about Swift here, so you're obviously going to want to choose Swift. So I'll select Next and once again I'll have to choose a location. I'll just select my desktop. And then here you go. On the left-hand side, you have your Project Navigator and I can look. I have, like I said, very, very little extra has been loaded here for me. All I have are two folders. My Sample folder, which is gonna contain all of my Swift code. And then ultimately my Products folder which is going to contain the actual executable that's going to get generated. Once all of my Swift code has been compiled. Now one thing quick to note is if you are intending on creating this application as it is, for use somewhere on your computer, you're going to need to know where this alternately is going to be compiled and created in, and that's gonna be in this full path over here to the right. Now, this is gonna be buried in here and I'll kinda show you where to go. But ultimately you might wanna pay attention to where this is. And also, another note is that if you want to actually compile this for use, you're gonna need to change the type here from a mock executable. To the default which is actually going to be basically just a Unix executable. So that's something that you're going to want to do as well, but just kind of make note of where the full path is and what type you're ultimately going to be creating here. So now I can come back to my main Swift file, and here we go, we have a basic Swift file. Now I can put a bunch of Swift files in here. And ultimately create a full application and compile it and we'll be ready to go. Now the reason I urge you to use Xcode and create a Swift project is because we're gonna get the full capabilities of the Swift compiler. So with the first two options with the REPL, and with the playground you're using the interpreter. And while the interpreter is not a bad thing to start to use to get used to these things, the compiler is much more powerful. It's much faster. It's more robust and it's gonna give you much better output as far as the bugging and the debugging tools and any sort of errors that come along. It's just a better more richer IDE experience where you're going to get all of the nice tools and capabilities along the way. So those are your three options, choose whichever one you would like, but at the end of the day I'm going to be spending most of the time creating these Swift projects and creating these executables. Where ultimately all of my code is going to be contained so that we can kind of get the full richness of the XCode IDE.