Since Swift is a type-safe language, it will do a lot of the work for you when it comes to making sure you’re declaring and using the right data types in your code. Learn how to efficiently take advantage of type inference.
- [Instructor] Let's talk about type safety and inference. Swift is a type safe language. What that means is that every variable that you declare needs to have a specific type, so the compiler can catch compiler errors down the road. Type inference will also come into play with mathematical operations, but we'll get to that a bit later. Now when you assign an initial value to a variable, the Swift compiler will infer its type without you having to specify it. So, with a type inferred variable we can just say var.
And we'll call this inventorySlots underscore inferred. We'll set this equal to five. Since we've given it an initial value of five, the compiler knows that from now on, inventorySlots underscore inferred, is of type integer, and cannot be assigned values of any other type. So if we try that, inventorySlots inferred equals, say, let's try and make it a string, hello, we're going to get an error.
Cannot assign value of type String to type Int. Go ahead and comment that line out just to remind yourselves of what we're doing. Now let's declare a variable called inventorySlots underscore explicit. Now we're going to add a type annotation, and how we do that in Swift is use a colon, and then a type. So this is going to be of type Int. On the next line, let's assign a value of 15 to inventorySlots explicit.
Now even though we didn't give the variable an initial value, we used a type annotation so the compiler knows that our variable is of type Int, and won't accept any values of other types. So if we try and make our inventorySlots explicit equal 15.5, it's going to give us an error. Cannot assign value of type Double to type Int. You can, of course, combine type annotations and initial values, but it's only necessary if that's your style, because having both is unnecessary for the compiler to do its job.
Here we can say equipmentSlots underscore superExplicit. We'll have this be a type Double. And we'll have the value be 5.5, although I don't know why you'd ever have a half equipment slot. Now just to note, we don't actually have to include the decimal value, or even a decimal value at all for a Double. If we want equipmentSlots underscore superExplicit to be of type Double, and we only assign it five, the compiler still knows that this is still a Double.
One last note. Since we can already create multiple variables on the same line, we should also be able to declare multiple variables with no initial values on a single line. So let's try that. Make some room, and on line 27, let's use var gold comma mana comma skillPoints. Here we can add a colon and make it of type Int. Now putting multiple variables on a single line like this, without initial values, will only work with a type annotation if all the variables are of the same type.
Otherwise, you'll get a compiler error. It's worth noting here that you can't use a variable before it's been initialized. So, for instance, if we tried to print out gold right now, we'd get an error. Variable gold is used before being initialized. So, here we would just have to say gold equals, I don't know, 150. And there we go. The debug area prints out 150.
Now we can, of course, do the same with mana, and with skillPoints.
- Starting new playgrounds and projects
- Variables and constants
- Writing single and multiline comments
- Core string methods
- Working with numbers
- Working with collections
- Creating arrays
- Working with sets
- Application control flow
- Writing functions
- Basic Swift classes and structs