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.

The 3 Basic Variable Types in Swift 4
Tagged on:         

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share This

Get your FREEapp monetisation flowchart!

Use our epic flowchart to help you find the money in your idea! If you're thinking of making an app then this is vital information!


You have subscribed! Please check your email to confirm this subscription and to receive your item!