Constants and Variables

As with any programming language, you're going to want to store information. In Swift, you have to make a distinction on if a piece of information will ever change. We do this with the keywords let and var for immutable and mutable data respectively.

let x = 3
// x: Int = 3
var y = 3
// y: Int = 3

Technically speaking, you could always use var , but it is good practice to use immutability as much as possible. Whether you are working with other people or are scared of yourself, let statements can be a great way of minimizing errors. Xcode will remind you to convert any variables to constants if the variable is never mutated.

Explicit and Implied Typing

Depending on what your programming background is, the above example may be slightly confusing. For Python users, the x: Int may be new. In Swift, a variable must always be one type. There are ways of creating a kind of loophole around this, but they're pretty advanced and rare. Moreover, data structures like arrays will also be restricted to one type–tuples can contain multiple types but must be consistent. This can seem limiting, but I promise this is a wonderful feature of compiled languages that will be very helpful. Since a variable only has one type, the compiler will be able to follow your code and will tell you if you ever violate type rules–and even sometimes help you fix them! So reiterating, the comment in the code is just to show you the type of the variable–it is not required.

Something java users may have picked up on is that you don't need to tell the compiler the type that is being assigned to the variable. This is true! Swift has implicit typing, which means that if an expression obviously evaluates to a type (like an integer literal or some other function) then that variable will be inferred to be that type. For the most part, this works very well, but if you have conflicting typing (a variable being inferred to be one type at one line and another type at another line) then the compiler can get confused and will not be very helpful. In these cases, or just if you want to, you can explicitly declare the type of a variable. We do this the same way as is shown in the type annotation:

let x: Int = 3
// x: Int = 3
var y: Int = 3
// y: Int = 3

Now, if I were to try to assign a Double to a variable of type integer, the compiler will yell at me:

var y: Int = 3.0
// error: cannot convert value of type 'Double' to specified type 'Int'

These type-annotations are the for variables and constants.

Basic Types

Some of you may be confused by the Double type. Doubles are floats, but with twice the precision. Quite literally, if you look at the memory required for doubles and floats they are 8 and 4 bytes respectively, which is not important. All you need to know is that doubles are the default for decimal numbers. All number types have literals–i.e. numbers with certain formats can be converted into that type. Of course, for integers, it cannot have a decimal. But Doubles and Floats have the same rules. If you want to create a Float instead of a Double, you can either initialize a float let x = Float(0) or do it with typing let x: Float = 0.

Common types are:

  1. Int

    There's a lot going on behind the scenes of integers, but unless you are doing memory-specific things (the only time I have ever cared is when doing graphics), this will not be an issue. For those that are curious, depending on the platform being used, Int will typealias (or convert) to either Int32 or Int64–32-bit and 64-bit integers respectively. For the most part you will be working with Int64 because Apple has switched to 64-bit systems. But, you can always directly specify if you need.

  2. Doubles

  3. Floats

  4. Strings

    Sadly strings are not as easy to use as in python. You cannot index into strings, but you can copy extensions from stack overflow if you need–it's not that hard to implement.

  5. Arrays and Sets

    See the Arrays page for more

  6. And your own types being Enums, Structs, and Classes

Wildcard

There will be some instances where you will want to bind a value to an identifier but will never use it. This will come in handy when you learn about conditional binding with optionals and enums. For now, we can think of this as executing some code and throwing away the result.

To do this, we assign the value to the wildcard or underscore _.

let _ = 3

Last updated