iOS Questions (Swift Programming Language)

 


1. What’s the difference between var and let


Both var and let are references, therefore let is a const reference.

let is used to declare a constant value - you won't change it after giving it an initial value. Its a immutable variable.

let theAnswer = 42

The theAnswer cannot be changed afterwards. This is why anything weak can't be written using let. They need to change during runtime and you must be using var instead.

The var defines an ordinary variable.

What is interesting:

The value of a constant doesn’t need to be known at compile time, but you must assign the value exactly once.

Another strange feature:

You can use almost any character you like for constant and variable names, including Unicode characters:

let 🐶🐮 = "dogcow"

let defines a "constant". Its value is set once and only once, though not necessarily when you declare it. For example, you use let to define a property in a class that must be set during initialization:


var is used to declare a variable value - you could change its value as you wish. var is a mutable variable
var changeit:Int = 1
changeit = 2

2. Difference between class and struct


3. var and let, Which one would you choose for properties in a struct and why?

Comments

Popular posts from this blog

Dispatch techniques: Dynamic dispatch and Static Dispatch

What’s the difference between var and let? Which one would you choose for properties in a struct and Class why?