Learning Swift #5 - Properties, Switch, Break, Function Names
Hey guys, this is Mad Heads 101, and today we're going to be playing around with some more Swift.
But before I get started, I want to mention that in the last four videos, those videos are pretty much my only Swift experience. Whereas in this video, I spent a little bit of time yesterday and today programming Swift just on my own. So I feel a little more comfortable with it than I did before.
The purpose of this video will be really to catch you guys up on what I learned while I was not recording, so that you can be caught up and continue to watch me as I learn Swift throughout the next videos.
So I'm going to get started and just create a playground, and I will call it "Catch Up." The first thing I'm going to talk about is custom accessors.
So first, let me just review what a variable is, or what a class is, you know, a variable. If I create something like this, I can assign to a number by doing dots like this, and I can get it by doing a dot like that. So it's basically just a property like it would be in a JavaScript object or with Objective-C if you had a public property, but it's just a variable.
So I can't actually do logic right now when you set a number and get a number. So, let's say I want to have custom code that runs whenever you try to get this property or whenever you try to set it. All I have to do is use these parentheses, and there are two different things you can do.
All you really need is a get, and then you can't assign, so I'll show you get first. All this does is you have some code here, and it just returns the value that you want the property to have.
So in this case, I'm making it a constant; it's 13. Whenever you try to access it, I could make it a random number, like if I do something like that. I think I need to do that as well. Okay, there's a way to convert it to an int. Here we go. So you can see here I just get some random number. It'll be different every time if I just start typing in here, so it keeps updating. You can see it's random, so I can have a custom getter.
I think I'll just return seven because I like seven, and you can also have a custom setter. So here I'm gonna print like "trying to set to," and this will get called whenever we assign to the variable, and it will do all the logic for the set.
So when you have a custom getter and setter, there is no longer any variable; it's just like a name. When they assign to it, it calls one function, and when they get from it, it calls a different function. So that's the gist of how custom getters and setters work.
There's also something called key-value observation, which is where I still... I actually do have a real variable now, and because of that I have to assign it a value. But I need to declare will set and did set. I'm not actually sure if I need to do both of them or if I can just do one, but we'll set to a value from...
What you'll be able to see here as I run this, and I always forget this close parenthesis for some reason, I set this to 3, and then I set it to 4. What is it complaining about? I don't need these parentheses. You can see that the assignment does work because it comes out as four.
But if I look in the output, it actually did print out that it was setting it to three, and it knew the value was still zero at the time. It was just going to become three, and here it was three, and it was going to become four.
So this is another cool way to have a property but to take some power over it and have your code get triggered when it's assigned to or read from. In this case, you can't do anything when it's read from, but assigned to you still can. So that's custom accessors. Now I'm going to delete this.
Okay, so the next thing I want to show you is something I learned about switch statements that I didn't realize before. So in the last video, I was talking about the tilde equals operator, and I wasn't exactly sure what it was used for, but in this example, I'm going to show you.
So let's say I switch a number, and the number in this case is three. I can actually put in here some sort of filter, or a list, so I can do three through five. And when I do that, I can print out three through five, and I also need a default. So whatever.
But here you can see that when three is a number, and if I figure out something, I’m just going to put a number here so that I can hit this plus so I can see the console output, it does say three through five.
So how does it know? This is an object; this is 3.5. It's actually... so let's say I do that; it's actually a range. So how does it know that this contains the three? Well, my number is not equal to three, that's for sure, or my range. How about I call it because... yeah, but that doesn't matter; it's not equal to three. In fact, you can't even compare it with three.
So obviously, the switch statement can't use this double equals operator. Instead, they use the tilde equals operator, which is kind of like "is three inside of this range?" and this returns true, which is why the switch statement works.
So that's why you need tilde equals instead of equals equals, because you need to have containers, and you need to be able to check if they contain this type or this object. But you also have, you know, number tilde equals number will just be equivalent to equals equals because a number isn't like a container.
But that's where there's a difference between equals equals and tilde equals when you're dealing with this container. So that is something I learned about switch statements, and I think it's pretty nifty.
The next thing I want to talk about is what I learned about function naming. So when you have a function and it's not in any class, the names of the arguments are actually optional. So if I have a number int, like price float, something like that, and I return how about float number plus price? Declare this as returning float.
If I were to call this, I could just call it 3 and 4.0, and it would work. But let's say I create a class, and I call it MyClass, and I... I don't even... I can just make this into a... yeah, I'll make it into a regular method just to illustrate the point better. If I try to call it now, I should actually get an error because it wants me to name the arguments.
So this is how Objective-C interfaces with Swift. So when I declare a class like this, it actually has to be compatible with Objective-C. So the name of this function isn't just "my function" anymore; it's "my function price," you know, like "my function with number price," however you want to think about it.
The point is that it has to interface properly with Objective-C, so the name isn't just this anymore. It also includes the name of all the arguments. So when you have a function outside of a class, because it doesn't have to interface with Objective-C the same way, it can just have, you know, you don't have to name the arguments because the name of the function you gave it is pretty much the only name you need.
So maybe that didn't make perfect sense, but that's something I learned about naming arguments: that when they're inside of a class, they have to be named, and when they're outside, you don't have to name them.
All right, the last thing I learned about that I think is a really great feature that not a lot of other programming languages have is the ability to break out of nested loops. So here's an example of where you might need that.
You might have three loops, and in the innermost loop, you decide that you want to break out of not just the innermost loop but the loop outside of that as well. So what I can do is I can say "outer loop," and here I can have my outer loop. And let's just make sure this is the right syntax. I can't do "1" there; I have to do "true." Now, and then I can say "middle loop." How about... how about I just keep making them all true and outer loop or inner loop while true?
So this is a really bogus example, but it's just for the purpose of example. All I have to do in this innermost loop is I can say "break middle loop." And so not only will it break out of the middle loop or of the inner loop, it'll also break out of the middle loop.
So here, if I were to print something... and I just realized this is bad because it's just going to run it in the Swift thing, and it's going to use all my CPU.
But this will never print if I were to get rid of this break statement; it would print. And it's printing a whole lot, so now I'm just going to delete this code so it stops using all my CPU.
But the point is, this allows you to break out of outer loops. So before, what you might have had to do is use a goto, or even, well, you can't use a goto in Objective-C because of automatic reference counting, really. But you could have... you would have had to set some variable to get out of the outer loop, and now you don't have to do that anymore.
So that is a really cool feature of Swift that you can name different control blocks, and then you can basically break out of them. So this pretty much wraps up what I learned in Swift while I was not making videos.
I hope you guys enjoyed this; I hope you found this as interesting as I did. Thanks for watching, subscribe, and goodbye!