DEV Community

Cover image for Swift 5 - Basics: constants and variables
Senik Hakobyan for Dragonfly Research Group, LLC

Posted on • Updated on

Swift 5 - Basics: constants and variables

Intro

Hello everyone!

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 electronic paper for learning purposes. Though I hope that someone will find them to be useful.

Constants and variables

Keywords: let and var

let pi = 3.14
var description = "Pi"
Enter fullscreen mode Exit fullscreen mode

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

age = 27
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

It is true for constants too:

let pi = 3.14, e = 2.718, phi = 1.618
Enter fullscreen mode Exit fullscreen mode

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"

print(description)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Though, we can't redeclare variables too!

var name = "John"

var name = "Bob"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

var firstName, lastName, country: String
let pi, e, phi: Double
Enter fullscreen mode Exit fullscreen mode

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

var message: String

message = true
Enter fullscreen mode Exit fullscreen mode

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

print(myString)
Enter fullscreen mode Exit fullscreen mode

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

PHP

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:

  • You can't define a typed variable out of the class.
  • You need to call the declare(strict_types=1); at the top of the script to be able to define types for function arguments out of the class.
  • If your argument is expecting int, then you must pass an integer... but you're still able to override that variable and assign a value of other type, e.g. string, just like in the example below.
class Foo 
{
    public function bar(int $number)
    {
        $number = 'Oops! I am not a number anymore.';

        return $number;
    }
}

$foo = new Foo();

echo $foo->bar(10);

// Output: Oops! I am not a number anymore.
Enter fullscreen mode Exit fullscreen mode

Though, in this particular example, return type can be a savior!

public function bar(int $number): int;
Enter fullscreen mode Exit fullscreen mode

In this case even if we are allowed to change the type of the variable, we are not allowed to return it if it's non-integer.

Outro

Well... I enjoyed!

Top comments (0)