Swift 4.2 Essentials

Introduction

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

This video covers the basic data types and how to declare them as constants or variables.

Keywords

  • Swift 4
  • Ios
  • Tutorial
  • Constants
  • Let
  • Variables
  • Var
  • Declaration by Inferrence
  • Declaration by Annotation

About this video

Author(s)
Mark Hoath
First online
17 November 2018
DOI
https://doi.org/10.1007/978-1-4842-4232-2_1
Online ISBN
978-1-4842-4232-2
Publisher
Apress
Copyright information
© Mark Hoath 2019

Video Transcript

Okay, so when we’re looking at our data that data can either be fixed, which is what we call constant, or it can change, which is what we call a variable. So, to define a constant we use the with keyword. So, we can say with my birthday there would be a string which is equal to 17, September 1968. So, that’s defining this constant name. My birthdate as a string and the data that’s going to hold is going to be this value here. Now, my birthdate is always going to be the same, but my age is going to get different, or it’s going to change over time. It’s going to get progressively higher. So, we need to create a variable, and variables use the ba keyword. So, we say ba my age which is an integer is able to 50 which is the value, and what we’ve been doing here when we’re been declaring these concepts in variables is we’ve been telling the compiler that the variable is going to have a specific type of data so that the compiler can allocate enough memory for these data structures even though they’re very basic currently, and that’s called declaration by annotation. That’s where we specifically tell the compiler what the data type is. So, we can say let annotated string, b, which is able to annotate it, and then the other thing we can do is the compiler is quite smart. And so, we have this other method called declaration by inference, and what happens in declaration by inference is the compiler infers the type of data based on what it receives from us. So, I can never remember how to spell inference, but anyway let inferred string equals inferred. So, here we haven’t said inferred string is going to be a string because we haven’t put this colon string after it, but the compiler sees that on the right hand side of the equal side there’s a string. And so, to make the left hand side equivalent it says inferred string must be a string.

Now, that’s really cool for a compiler, but declaration by inference is not really good for programmers, and it’s always good to let other programmers know, and even for you when you go back to your source code when you’re declaring variables or constants what the actual data type is even when it’s obvious. We’re going to show you some examples of why that’s the case. So, let’s say that let three is an integer, which obviously it’s a whole number, but three can also be defined as a float, and defined as a float we would say well the value is 3.0 but then let’s have an inferred three. So, it’s gonna be inferred. So, we don’t tell the compiler what type it is. If we put 3.0, what are we going to get? Are we going to get a float, or are we going to get an integer, and as it turns out we’re actually going to get a double, which is another kind of decimal number. And so, in the cases where you don’t specify what the, when we’re using declaration by inference, and the value here is decimal the compiler will default and say this is a type of double, and we’ll get to those here shortly. So, in the ex-code playground here, and I’ve hit the execute playground button here on the left hand side, and you can see here on the right hand side it’s filing in the data, and we’ve got integers, and we’ve got strings, and we’ve got thrill up to three.

So, create a quick little test function, and we’re going to create a constant let placeholder equals counter, and we’re going to create a variable equal to one. There’s obviously going to be an integer, and then we just want to print the placeholder, and print the counter, and you can see the compiler is pretty fast. It comes up with these yellow messages saying you haven’t used this. So, you might not need it. This one here says variable counter was never mutated, and mutated is another word for changed. You should consider changing that to a let, but what we’re going to do is we’re actually going to change it and increment it by one. The other thing we’re going to do is we’re going to change placeholder to say incremented, and let me print that again, and we’ll run that, and you can see what’s happened here is we cannot assign to the value placeholder. So, we can’t change this value placeholder because we’ve defined it as a constant using the let keyword, and if we do want to change it then we just need to declare it as a variable. Once we’re done that the error message disappears, and now that we’ve completely defined our function we can run it like so, and there are lines running counter one. It’s printed out, counter with a return, and one with a return, and we’ve incremented our counter by one. We’ve got two, and change it to incremented, and down here in our debugger it’s exercised and printed all around print and commands.

So, that’s how we define variables and constants. So, let’s have a look at the basic dial types that we would use to start off our projects.