A variable is essentially a container that holds a piece of information. In Swift 3 these are not much changed from earlier versions of Swift.

But let’s go over them anyway šŸ™‚

Can I change a variable?

There are 2 keywords we use to describe whether a variable can be changed or not:

Var implies ‘variable’ which means you can change it later on. Example:

Let is not changeable later on. Example:

Let STOPS people (including you) from messing up constants that shouldn’t be changed (or mutated to be grammatically correct)

Strings

A string holds a bunch of text, just like you might expect. In the above examples we declared strings ‘implicitly’. Ie, Xcode figured out that the variables enclosed in ” ” are strings (same in most other languages). But what if it’s not so obvious, or we want to ‘explicitly’ tell Xcode that we definitely want a string type? Example:

The great thing about this approach is that there is NO confusion about the variable type!

Numbers

There are many types of numbers in all languages, primarily for reasons of efficiency I believe. The simplest is an integer (a whole number).

Then we get the next 2 most common numbers: doubles and floats:

Clearly these can hold decimal values, but what’s the difference between them? Double can hold about twice as many digits compared to float (impressive, double, very impressive). If in doubt then double is preferred, Apple said so!

Booleans

Sounds like an alien race doesn’t it? Well they’re much less exciting than that! A boolean holds a true or false value. Eg:

Insanely useful for keeping track of application states. Stuff like:

Is the oven on at home? (

—-> YES!

And when it’s switched off:

Is the oven on? (

—-> No, the kitchen won’t burn down!

Well, those are the 3 basic variable types you’ll be using a lot of in Swift 3. There are many many more but once you know the basics (as outlined here) you should find them super easy to use.

Where to go Next

This tutorial comes straight out of The Complete iOS Developer Course. Over 40 hours long it contains everything you need to create awe inspiring apps fast!

Click here to check it out!

The 3 Basic Variable Types in Swift 3

Leave a Reply

Your email address will not be published. Required fields are marked *

Share This

Want this Post in Easy to Understand Video Form?

Then get theĀ Complete iOS Developer Course. Over 50 hours of pure Swift coding goodness designed to turn you into a coding master!

One Time Offer - $9! Click to Get it!



WARNING: Once this box is closed the offer will not be shown again.

Thank you!