Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
FREELessons:30Length:4.8 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.5 Demo: Addition Calculator

Now that you have a grasp of the basic concepts of Swift, let's create a simple application. We're going to create an application that takes a couple of numbers from the end user, adds them together, and outputs the result. It sounds simple, but there are a few new concepts and ideas to consider.

2.5 Demo: Addition Calculator

So at this point we've started to learn enough to be able to start to put some concepts together, and maybe create a very simple application. So that's exactly what I'm going to do here. So as you can see here, I've created a very simple project called Add. And the basic process behind this, or the premise behind this application, I'll be quite simple, is to ultimately take two numbers and add them together. Now that might seem a little bit boring and a little bit beneath us, but really I'm going to add in a little piece of interesting flair to this, so that you can actually maybe get some use out of this and learn another little trick. So what I wanna do is I wanna be able to take two numbers. And I wanna be able to take these two numbers from the end user somehow and add them together and produce the result. So it's a very primitive addition only calculator if you wanna think of it that way. So what I could do is, I could go on here and I could clear this out. And I could say, all right, you know maybe let first=five, and let second. But that's kind of boring. I want to actually get some input from the end user. So how do I do that within Swift? Well there's actually a couple of different ways. I could start to import foundation and there's some ways that I can get at the command line arguments, cause ultimately remember I'm just creating a bunch of command line applications here. But how do I get access to those command line arguments. Well, it's actually pretty simple really. There's actually a built in struct. Now we haven't talked about structs yet and we're gonna get there in an upcoming section. But really all it is, is a way to contain all of the data or a number of different pieces of data into one object and in terms of Swift for the command line, we're able to actually get at the arguments that are being passed into our application from the command line. And the way that we do that, is by accessing the process struct. And within there, we have an arguments property here, as you can see here by these square brackets and the string in between. We know what that is. It's actually an array of strings. So I can get access to the information that's being passed in here. So I'm going to say var numbers. Are going to be equal to the process arguments. So its going to take the arguments that are passed into our command line and save them into numbers so that's pretty nice. So I have an array here, so let's start to work with this a little bit. So now I want to make the assumption at this point that there is only two numbers here. We haven't really learned enough to be able to get into all of the different pieces of information and loop through maybe an array of n number of pieces of information. We'll get to that in the upcoming lessons. But for now, let's just assume that we're gonna be dealing with two numbers. So I'm gonna say I want to let the first number be = numbers. And remember, this is an array. And I'm gonna say, I wanna get the first one. And then I wanna say let second = numbers and I want that to be the second number that's passed in. All right, that seems pretty reasonable. Then I'm gonna create the sum. Let sum = first. This shouldn't be too complicated. You should see how this is all coming together. And then I'm going to come down to the bottom and simply do a simple print statement. And I'm going to use a little bit of a trick here called string interpolation. Which is going to allow me to actually kind of concatenate multiple strings together in one line, which is a nice little trick and you're gonna see this quite a bit. So what I could do here, is I could concatenate all this stuff together by saying the sum of, and then I could say + sum +. And I could continue doing stuff like this, but this is kind of a nuisance having to hit the plus and create new strings and all that kind of stuff, and when I just want to write it in a single fashion. I could say the sum of, and I'm going to show you a little trick here. I just like to write out the full string that I'm gonna deal with. The sum of first and second is sum. The way that we actually inject these values in here is by using this string interpolation. And the way that I do that is I use the backslash, open parenthesis, the name of the variable and then closed parenthesis. So now what's going to happen when it prints out this string is its going to inject the value of first into my string here and print it out for me. So I can do the same here with second, I can do backslash, open parenthesis. And then second and close parenthesis and then finally sum like so. So we'll go ahead and save that and we'll build and luckily for us it builds. Now we wanna actually run our application so how do we do that? Well, we can use the menus on the top or I like to use these shortcuts. I could use command r. Now, I'm gonna warn you ahead of time this is not gonna work as you would expect, and it's actually not gonna work at all. So what exactly happened here? It looks like it errored here on letting second equal to numbers one and I can kind of hover this and you'll see that, well numbers only has one value and this sure as heck isn't a number, so what the heck is going on. This is kind of your first foray into the world of debugging a little bit. I see here that I was able to get through this line so it got something in. And like I said, numbers seems to have one value in it. At index 0 which has this really long goofy-looking string. So what exactly is going on here? Well, the problem here is because we have created a command line tool, and we're just running this in the debugger, we haven't really had an opportunity to pass in data at the command line. So what you could do at this point, if you really wanted to, is you could go on hunt down the full path of the executable. Remember I could go under products and add, and I could find it all the way buried down in here in this really crazily named file. But that would kind of limit me in my debugging work, I'd have to do it at the command line and then come back into Xcode. But I want to continue to debug in in Xcode, so I could go back into my code file and instead of doing it that way, I can actually tell Xcode that when I run this particular executable, I wanna pass some arguments in to it. Okay, so let's try and do that. How do we do that? Well I'm gonna go into product and I'm gonna come down to Scheme, and I'm gonna select Edit Scheme. Now the world of Xcode is a very deep and windy road. And I'm not going to spend a lot of time going through this. Because I could spend years discussing all the different nuances you will find within Xcode. But just kind of take my word for it. That if I were to go into my schemes and select run here, I can come in here, and I can specify arguments that are passed on launch so. Like I said before, I want to assume that we're dealing with two. So we're gonna pass in a, let's just say a two, and we'll pass in also a one. So we're gonna pass in two values, two in one, so I'm assuming at this point that if my application were to run successfully, the output would say the sum of one and two is three, is what I'm looking for ultimately. So we can go ahead and close this out. So now we know we're passing in some data. So let's try and run this again. So Cmd+r. So we're going to get, we got some crazy stuff here. The sum of this really long thing and I can't even read this, it's unruly. Well if you remember before as we started de-bugging this we noticed that something strange was entering in to our numbers here that maybe wasn't a number. So now that everything is running correctly, how do I actually get some information? And how do I actually do a little debugging? Well the first thing that I can do is I can actually set a break point. So I can come over into this little gutter here on the left, and I can click on a particular line. And I’m gonna get this little blue arrow, and this is a break point. So now if I were to run my application again, you are going to see that my execution is going to stop on that line and I'm going to get access to the information or the, all the different variables in place in memory that I'm working with here. Now I haven't actually done anything yet so If I take a look at numbers you are going to see it's empty. There's nothing there yet. Now if I come down my little toolbar here you are going to see a number of different buttons here I can continue, or run to the end, or run to I hit another break point. I can step over a line, I can step into a line. Or I can step out. So in this point, I want to step over. And as you can see now, I have numbers here. So now I have three numbers, and as you can see they're not all numbers. I see our two and our one. But what's this first one? So what's actually happening here is the first argument that gets passed into this structure is actually the name of our application or the location of our application itself and then the command line argument. So were not doing anything wrong necessarily here but were not really doing it right either. So I wanna stop this. So what's happening here is we're passing in two numbers but they're not at index zero and one. They're actually at index one and two, and that's what I'm looking for. So lets try this again now. So I'm going to remove this break point. I'm going to actually right click on it and I'm going to say delete this break point. You can also disable it too and then re-enable it as you want, but I'm just going to get rid of it. So let's go ahead and run our application again. This looks a little bit better. So it says the sum of 2 and 1 is 21. Well I know that's not true, how did that happen? Well if you were to kind of pay close attention to when I was working with this arguments before, is that arguments is actually, and if i were to ALT and click on arguments, you're going to see here that it's actually an array of strings. So when I got back into numbers or actually strings, so these values in here are strings, and so this operation didn't add two integers together. It concatenated two strings together. So if I wanted to get the integer version here, I could say int, and we've talked about this before. I'm gonna specify the string within int here for both sides. And then the ultimate result here is that I'm gonna hope at least at this point that it's sum will get the sum of the integer conversions of first and second, and it looks as though something isn't happening very well. And that's because if you recall, these are optional. So when I run this operation, I'm going to get optionals here. Now you can do some error checking in here, and do a little bit of defensive programming, and I would definitely leave that as an optimization for you to do. But at this point, I know that the values being passed in are going to be actual integer values in the form of a string, so I'm actually going to use the exclamation point on both of these to unwrap those optionals immediately, so I know I'm gonna get the value out that I'm looking for. So I can now do Cmd+R, and I'm gonna get now the sum of two and one is there. So there you go, now you've spent a little bit of time working on creating an actual application, all be it a simple application, but a very interesting one none the less. And you learned a couple new tricks about the process.arguments, the process strut with the arguments property that contains the array of data that's being passed in as arguments. How you can get the data out of them using a raise. How you can convert those strings into integers and then unwrap the optionals if you know it's actually going to be an integer value. And then we learn a little bit about string interpolation and being able to inject values into a string without having to add any of those strings together. And you learned a nice little trick about going into the product and editing your scheme to actually pass in some command line data. Now that's definitely a lot of very cool information that I think you're gonna be able to use while you're starting to or continuing to learn about the Swift programming language.

Back to the top