Category: Swift
Tags: #swift #php
Published: 2020-09-26

Swift 5

Swift 5 - Basics: constants and variables


Some time ago I've decided to start learning Swift by writing posts about it. So, this is the first post...

I'm not trying to write "tutorials", it's not true. These posts are just my thoughts on the electronic paper for learning purposes. Though I hope that someone will find them to be useful.

Constants and variables

Keywords: let and var

Finally, I saw clear boundaries between let and var keywords... No, not in JS, but in Swift!

let pi = 3.14
var description = "Pi"

The keyword let is used to define a constant. The keyword var is used to define a variable.

age = 27

If you won't specify the keyword, like in the example above, you'll get an error - Use of unresolved identifier 'age'.

You don't specify the var keyword only if you're overriding an existing variable.

You can define multiple variables in one line:

var name = "George", age = 27, position = "Software Engineer"

It is true for constants too:

let pi = 3.14, e = 2.718, phi = 1.618

You can override variables in Swift, but you can't override constants.

var description = "Pi"
description = "Ratio of a circle's circumference to its diameter"


So, the output will be "Ratio of a circle's circumference to its diameter", because the variable description is overridden.

let pi = 3.14
pi = 3.1415

The result is an error - Cannot assign to value: 'pi' is a 'let' constant.

How about redeclaring a constant?

let pi = 3.14

let pi = 3.1415

The result is another error - Invalid redeclaration of 'pi'.

Though, we can't redeclare variables too!

var name = "John"

var name = "Bob"

Getting the same error message - Invalid redeclaration of 'name'.

So... we can't override or redeclare any constant or variable we have defined. I like it.

Type annotations

Swift lets you to provide type annotations for declared variables and constants. Exciting!

I like type annotations, because they improve the readability of the code.

var message: String

message = "Swift is cool."

let pi: Double

pi = 3.14

You can define multiple variables on a single line that have a same type:

var firstName, lastName, country: String
let pi, e, phi: Double

Let's try and violate the type annotation by assigning a boolean true to the variable message.

var message: String

message = true

Got an expected error! Cannot assign value of type 'Bool' to type 'String'.

Attention! Your variables always have a type, even if you don't have type annotations provided. Let's see another error example.

var myString = "some string"

myString = 777


Guess the error? Cannot assign value of type 'Int' to type 'String'.


At this moment PHP is my main instrument, and whatever I learn - I compare it with PHP...

In PHP 7 we have type annotations for functions, class methods and properties.

Anyways, I like type annotations in Swift more than in PHP, and there are various reasons for that:

class Foo 
    public function bar(int $number)
        $number = 'Oops! I am not a number anymore.';

        echo $number;

$foo = new Foo();

echo $foo->bar(10);

// Output: Oops! I am not a number anymore.


Well... I enjoyed!