yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Learning Swift Attempt #1 - Creating a Calculator


15m read
·Nov 3, 2024

Hey guys, this is Mac Heads 101, and today I thought I would learn some Swift. Um, if you're not familiar, Apple just released their new programming language Swift, and most like, uh, pretty much every other iPhone developer that exists, I don't know Swift because they just came out with it.

So these videos aren't really going to be tutorials as much as me trying to figure out how to use Swift, and for those of you who want to learn, maybe this will help you as well. Um, but so basically, these videos are going to be completely informal. I might be Googling stuff during the video trying to figure out how to do stuff. I'll probably get pretty angry at stuff that doesn't work.

Um, but anyway, let me get right into the video. So I'm going to be attempting, I think, uh, to just make a simple calculator in Swift this video. Uh, so I really have no idea what will go into this. I haven't even used Xcode in a while. Um, but I'm going to go ahead, I think I still go to iOS app, and let's see, probably single view application, and I will call it bad calculator.

And I, of course, have to switch Objective-C to Swift, and I think that's it. I don't see any class prefix anymore, but I guess they got rid of the need for that with Swift, so let's go ahead and create that. And okay, I immediately get an error, so I'll just click okay, and I think we're fine. That was strange.

And of course, I'm not going to test my code because, um, what kind of programmer actually makes sure their code works? Not me. Um, so now let's see if I can edit the view controller. So, hm, it's pretty wide. I don't know why it's so wide since the iPhone isn't this wide. Let's just see what happens when I run this.

So I'll go ahead and run it on an iPhone 5S simulator. I guess this is neat; they have a resizable iPhone simulator, huh? Okay, so how do I run it? Let's see. And of course, it wants to connect to the internet. More stuff wants to connect to the internet. More stuff wants to connect to the internet. More stuff wants to connect to the internet. Wow, look at this amazing suite of networked programs Apple made.

Okay, so here's the app we have so far, and I suppose it's just a blank screen. So I suppose I will quit the iPhone simulator and go back to our view controller and let's see what we can figure out how to do. So for some reason, I have to save in here even though I didn't change anything.

Um, so what happens? How would I add buttons? I assume I can just do this. However, this view is a lot wider than the actual iPhone. So what happens if I run this right now? And I will have you note that right now I'm not even using Swift, I'm just trying to figure out how to use this IDE.

Okay, so it looks like the buttons are there. Let's just make our buttons for the calculator buttons. So I suppose copy that. Well, did not mean to resize that, and I'll speed up this part because I don't know how to generate this with code yet since I don't know Swift. By the way, I expect in no way that this app will be good; however, it would be nice if it even worked.

So here I'll just write zero because it'll start out with some zero. And what can a calculator do? I guess you can add, right? Oops, that is not an add button. And you can also subtract. Maybe I'll just do add and subtract for now, we'll try later to add more.

So what happens if we run this now? I hope this is all in the screen. I really have no idea why this is wider than the actual iPhone view. Okay, so it looks like we have buttons now. All we have to do is figure out how to get our Swift code to edit this and get events from this, and I will consider myself done with the gooey.

Although this looks pretty god-awful, but, uh, whatever. It doesn't even fill up the whole screen, it's probably unusable because your fingers are too small to press the buttons. But let's go ahead and figure out how to create what are called IB actions. So I assume it's still IB action Swift.

Ah, so here I guess is Apple's code, and here's an IB action. Interesting. So okay, let's make this. How about one? How about we just make it called button tapped? And we'll go back here, and I will again, I'll probably just hook up this one first to make sure it works button ta.

So let's see, does this work? I hit one, it prints out button tapped. Okay, that's pretty cool. So let's go back here, and what I'm going to do is I got to figure out how I can make sure this is a button. Can I just do that? Is that right?

And let's see, I'll name that the button. Do I have to rehook up? This is this still hooked up is the question? It is. Okay, so let's go back again. Again, let's see if we can figure out how to print out the title of the button so that we can see if it says like one or two or something like that.

So here is the button object. Uh, that's going to be pretty crappy, but at least it works. There we go, so we get it to print out one. Now I'm going to hook up the rest of the buttons to the same event because I don't want to create nine methods for each button.

So, um, yeah, ideally I would be generating this with code, but that can be for a future video once I actually know the programming language. Aha! So now if we run it again, it should print out all the numbers in order since that's the order in which I'm tapping them. And it did great.

Now I'm going to make separate events for the plus and minus. So I'm going to copy this because, yeah, and I'll hook up those. So here we go, bring that there, minus tapped and plus tapped. By the way, to make these connections, I already knew this from Interface Builder because I've used it before.

All I'm doing is holding control or right-clicking while dragging, um, and so now I've hooked up these. So let's just say, and I assume this will work. Oh, and I guess I don't need a semicolon here, do I? I don't need that. It's going to get hard to get used to not typing semicolons in Xcode.

Let's go ahead and do these, and then they worked? Did they work the right way? Yeah, I hooked them up correctly. Now we got to figure out how to do an IB Outlet so that we can access this label. So let's go back here, and this looks pretty simple.

Let's create the outlet, and of course, we want a UILabel, so answer field. And it's giving me an error but not anymore. And there we go, I can go voila.

Okay, so we have everything in our terrible gooey in which I didn't know how to use Interface Builder. You know, let's look at this. How do we like, what is, ah, this is an iPhone? Okay, so if I make it look like this, I can see what the iPhone looks like. How about I'm just going to quickly fix the gooey a little bit and make it use the entire screen because really, why not?

And maybe I'll make this taller and give it a background color just so you can see it's the answer. This is really what I like messing around with, the gooey. So background, that's not good. How am I going to read it? How can I set the text color?

Color, I want it to be white, and maybe I can set the font to some weird calculator font. I could go through all the fonts, but that's not worth it. Why would I spend my time doing that? You know, I'll just set it back to, um, something that I know what it is, like Quer new, and maybe I'll center it, and that'll be the end of it. And why isn't it?

Maybe I'll make it bold, yeah, bold. I'll make it the system bold font. Yeah, that will be fine. So we already have that hooked up, and we have everything good. So great. Now I'm going to go ahead and start implementing this. So what I think I want to do is make it so when you hit a number on the keyboard, and oh, I forgot a zero key, didn't I? Yeah, that would be important.

H zero key, and is this already hooked up to the outlet since I copied and pasted it? Yes, it is nice. Let's just make sure of that. So now when I hit zero, um, it logs out. That's fine. Okay, so we got to make it type a number.

What I'm going to do, I'm not even going to make a separate variable, I don't think, because mainly I don't know how to do that. But instead, I'm going to make it so if answer, oh yeah, how do I do an if statement? That's a good question.

Text, do is, how about e? Uh, how do you compare a string equals? Ah, wait, compare string and Swift, maybe they overloaded the equals operator so I can just use that. You think that's what they did? Ah, so you can use the equals operator, and that's fine.

I think I'll just use the equals operator. So if answer field. text is equal to Z, how do I, yeah, I think that's it. And answer else, so what you're going to do is if the field is zero then, uh, how does regular calculator work? Let's just look at this.

So every time I type a number, I puts it on the right side, so yeah, if it's zero, we'll just type the number that they just put in. So answer field. text equals the button. title label. text. Okay, and otherwise, somehow we need to put the number they just typed to the right of whatever text was in there before.

So can I literally concatenate the string like this using plus equals? If they have operator overlook loing, okay, so I can't do that. I don't want to assign it, can I do that? Yeah, okay, that's annoying that I couldn't do plus equals, but that's fine.

So let's go ahead and run it again. And voila, it's allowing me to type a number. Oh gee, I should make a clear button too. So I'm going to make, I don't care what they pass. Oh, okay, so Xcode froze for a second, printed that out.

Feature of the beta, I mean what can you expect, right? It's just coming out and it's not even in production yet. So let's center that and make that run our clear tapped method.

So now if I type like 55, and why does it say dot dot dot there? That's not what it says here, that's not cool. Let's try it again. There we go, so it goes back to zero, what we wanted. Now we should also probably, so when you type minus and then you type another number, you got to be able to hit like an enter button or something.

Uh, so let's make that. So this will be enter, this will be clear. You know, it just struck me that the apps are further from the left side that the buttons; they aren't really on the right side, that's pretty weird.

Yeah, okay, so we've discovered that I don't know how to use Interface Builder. That's, we already knew that. So we have our clear and enter buttons. [Music] And there should also be some label indicating what operation we're currently doing, so that'll be empty by default.

And I want it to not be teeny. Oh God, what is happening? Oh goodness gracious. Oh my, oh jeez, okay, so clearly that was a mistake. How do we make just a nice label with no text in it? There we go, just what we wanted.

Let's go back here, make another IB Outlet so we can access that label, and let's make another IB action. It annoys me that when I type half of IB action it doesn't give me a suggestion, um, for the rest of it. That's fine.

So like, how do I type a comment? I guess SL slash, yeah, right there, to do this. That's what I like to do when I'm coding; I just write a comment in every single method that says to do this so I know I have to implement this method, but I'll come back to it in a minute.

So let's see, when I hit minus, I think what's going to happen is if they were already doing an operation. So for both minus and plus, if they were already doing an operation, they typed a number and then an operation and then another number, um, what should happen is it applies that operation and then goes to the next operation.

So how about if operation Operator, Operator, not operation. Okay, whatever. Text is equal to, and I don't need the parentheses around the if statement. Uh, if it's equal to an empty string, now here's a question, will it be nil sometimes?

Yeah, what is this exclamation point there mean? Does that mean it can be nil sometimes? I really haven't the slightest clue. Okay, that's not even what I wanted to do. Operator label. text is nil. Is that allowed?

Ah, finally I get a thing. Cannot compare a string to nil, so it will never be nil. That is exactly what I wanted to know. I hope it'll never be nil. I don't know what the exclamation point meant.

All right, so if the text is that, then we have been doing no operator and operator label. text will just get to equal minus. And I think I'm going to make the answer field. text go back to zero so that you get to type the next number.

Otherwise, we will apply the current operator, so enter tap will apply the current operator, I guess. And can I pass nil to it? Expressions take that to any object. What if, all right, I give up.

I give up, like what if I do any object at question mark? Ah, question mark, did it? Don't know what it did, but it did it great. So enter nil will allow us to make it look like they tapped enter, and then we'll go back to this.

So when they tap enter, we set the answer field back to zero, but first, we'll do a calculation. So actually, here's the thing, we need to save the last thing that they did, so last number will be a string. Whenever we set this to zero, we have to get the last number.

Whenever they clear it, I'll set the operator field back to nothing, and I'll set the last number back to nothing as well, and sweet, Xcode stops highlighting my stuff for a second. Now it's complaining here.

Can I initialize this to be an empty string? Yeah, good. Okay, so when they tap enter, say if operator label. text is minus, I'll do a subtraction operation. Else if oper, wow, I really don't know why Xcode is doing this, uh, nice thing here where it's going black and white on me, but that's not great.

Yeah, so that is not something I want to do. Okay, so we're subtracting; this should be easy. Let's just figure out how to do this. So we're going to say answer equals last number dot how do I parse it as an int?

Okay, we need to turn the string into an int. Ah, see, Stack Overflow is really the best for this. To int, okay, that's what I want. And what is this int question mark? Does that mean that it could return nil, like if it doesn't work? That's kind of cool.

So how do I do this? If not answer, uh, okay, this indentation thing is getting on my nerves. You guys better be seeing what it's doing right now. This is not okay. But, yeah, so if can I do if bang answer?

Yeah, so if bang answer, how about I make a new function? Like show error, I'm going to show error, and then I'll return. Okay, oh, you know what? I'm going to do, I'm going to parse that up here.

So last number num one will be okay. Wow, this is really, I mean, I don't know how anyone would code like this with it just constantly going black and white every time I type something, but I'm sure that is just a temporary feature of Xcode.

All right, so if I can't parse the last number, and I'll also try to parse this second number, so answer field. text. To, um, and of course, I actually don't know if num one is zero or num two is zero, will it show an error? I don't know, and I got to remember to do this.

There's got to be a way to show alert window or something. Okay, but let's see, so I get the answer. The answer here, how about I start off the answer being zero and I say answer equals num one plus num two and answer equals num one plus num two.

And otherwise, I will show an error and return because they didn't have an operator in when they hit enter, answer field. Somehow I need to turn a number back into that, so I think I can just do that to turn it into a string.

So many errors, so many problems. What is it? Why? What? No, what? To int? It's complaining about that. Do I have to put an exclamation point after these things? Like, wow, really? I really meant num one and num two?

Yep, I actually do need to do that. Now, what's it complaining about here? Plus, okay, can I do like answer dot to string? No. Uh, string to int Swift? No, in to string Swift? No. No, int to string?

Uh, EDS string equals 1, 23. Ah, if we just do answer as string, does that work? Wait, why is answer that expression?

Okay, this is an int. Why is it still complaining? What have I done? This expression apparently answer is some magical thingy. Like, do I have to do that? I don't even know what that does. What a, okay, what if I just try to do this?

Will that work? Let's just try to run the app. Okay, so if I type one, two, I hit plus, it says plus tapped. Oh, I forgot to implement the plus thing; I just did the minus thing, so there we go.

So the enter button is still small. There was an error that really tells me nothing. I hit, wait, why it, what is operator label right now? Operator label. text, is that the problem? Let's hit minus; that's where I put this log.

If I hit minus right now, oh, I hit enter, that's why. Enter, of course, it's going to give me an error; I didn't type an operation or anything. Okay, so first off, let's figure out why that was happening. I figure out. I mean, just make it wider and hope for the best.

So there we go, seems to be working. Let's do 25 minus 3 enter, and it print out 22. I will give you that it did it; however, after I hit enter, it didn't set the thing back right. So we didn't figure out how to set the text of the answer label, so answer field. text equals answer as string.

Did that work? No, answer as string, exclamation point? No? Okay, let's, it's time to Google this error Swift.

Okay, and of course, Google doesn't recognize the, ah, I saw the error we just had. Why is it complaining? Yeah, so I don't know what this is supposed to mean, but it's not a very verbose error message.

Can I do string and then in parentheses my answer? Just getting rid of that. Build failed, uhh, are there any suggestions? Okay, literally this is all I want, want to do. Set label, set UILabel text to integer Swift.

Okay, like, I don't even know, like this guy literally just posted a ton of code and the answer was that. So I'm not g to no, okay, this might have it. Yeah, this probably has it. Oh nasty, what? That's cool.

All right, so if I just do this, will it work? Yes, wow. So I really don't know why it didn't work before, but this is kind of cool.

So I guess when you have a string, if you have back SL left parenthesis and then some expression and then right parenthesis, it converts the thing in there to a string; it's like magic. So let's see, 25 minus 3, it's 22.

What it should do is get rid of that thing right there, and last number should go back to being nothing, although that doesn't really matter, does it? 25 minus 3 plus 9. Wait, what? I hit plus when I hit plus; it didn't do what the minus thing does.

Yeah, this is just a problem with me sucking. So when I type plus, it sets it to the answer, and ideally, when I hit nine, it would change the text to be the next number, but, uh, it's too late for that.

I don't feel like rewriting the calculator, so this isn't very good as a calculator. Was that right? Yeah, I think I just typed 885 minus 5; it better be right, and I guess it works with negative numbers, although there's no way to type a negative number.

You have to do 0 minus whatever, 58, and then you can get minus 58 and then you can do plus 96 and then get an answer there.

Okay, so that was me trying to write a calculator in Swift. I struggled a lot, and I learned very little, uh, but hopefully, you guys learned something, and I learned something too. I still don't actually know what would happen here if we tried to parse a number that wasn't a real number.

How about we try that real quick? What if I, I'm just going to do it in viewed load. I'm just going to see var number equals k. 2 in and what happens if I do var answer equal number exclamation point plus three? What's going to happen?

Because what I feel like is this is an INT exclamation point, right? And, or maybe it's an INT question mark. What is it? An INT question mark?

So in order to get the real number out of it, I have to use an exclamation point or else I'll get an error. So, but it's not going to work because it should be nil, uh, fatal error can't unwrap optional. None, so you got to check if it's nil first.

So if bang number, else, this is kind of cool. So I learned something. Actually, it's nil. Does this work? Oh yeah, okay, so I actually, that's kind of cool.

So I understand optional values a little bit better. Still don't truly know what that exclamation point thing is. Uh, maybe I should look that up real quick. Exclamation point after type Swift.

Okay, once you're sure that the optional does contain a value, you can access its underlying value by adding an exclamation mark to the end. Okay, well that's what I just figured out.

Um, so that's using an exclamation point, and we just saw this runtime error that they're talking about. Optional binding.

Okay, where else is there an exclamation point? I really, here we go. Okay, no, exclamation mark is needed to access its value. It's an explicitly unwrapped optional string. What is the difference between that and just a regular string?

You can think of an implicitly unwrapped optional value as giving permission for the optional to be unwrapped automatically whenever it is used rather than placing an exclamation part after the optionals name each time you use it.

Uh, so it's basically an optional value that better become non-null pretty fast. Yeah, I just see no value in that. Whatever, here's assertions.

So I'll probably be making another video pretty soon because I enjoyed this, and I like learning Swift. And I think you guys probably enjoy laughing at me, um, even though, um, you know, it's probably hard to watch, especially if you actually know Swift; you're just watching me struggle.

Uh, but anyway, uh, this was me trying to learn a little bit of Swift and making a really crappy calculator. Uh, so thanks for watching Mac Heads 101. Subscribe and go bye.

More Articles

View All
Could this technology end all viruses?
This round structure is only about ten billionths of a meter in diameter, but it— as well as other technologies in the pipeline— could be stepping stones to a monumental public health ambition: a single vaccine that protects you against everything. We’ll…
Character actions in stories | Reading | Khan Academy
Hello readers! Today it is a time for action. Yes, sound the horn of action, because today we’re going to be talking about character actions in stories. Understanding what characters do is key to your success as a reader. The way characters behave towards…
Ask me anything with Sal Khan: March 27 | Homeroom with Sal
Hi everyone! Welcome to our daily live stream. This is why we’ve almost, we’ve been doing this for a little bit over two weeks. For those of you all who are new to this, the whole point of this is Khan Academy is a not-for-profit with a mission of providi…
More Than a Grand Canyon | America's National Parks | National Geographic
[MUSIC PLAYING] [BIRDS CHIRPING] The Kaibab Plateau is home to one of the last ponderosa pine forests in the southwest. It’s a unique habitat, protected by Grand Canyon National Park, and a spectacle to behold. [MUSIC PLAYING] Less than 5% of the 6 mi…
National Geographic digs into the history behind Indiana Jones and the Dial of Destiny | Nat Geo
HELENA SHAW: Dad told me you found something. A dial that could change the course of history. HARRISON FORD: With Indiana Jones, I always thought that what would be interesting, is to see this educator adventurer fooling with the nature of science. JAME…
Horizontal tangent to implicit curve | AP Calculus AB | Khan Academy
We’re told to consider the curve given by the equation that gives this equation. It can be shown that the derivative of y with respect to x is equal to this expression, and you could figure that out with just some implicit differentiation and then solving…