Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How properly declare a variable in Swift?

I found quite interesting these different ways to declare a variable in Swift:

// METHOD 1
var dogName: String = "Charlie"

// METHOD 2
var dogName: String {
    return "Charlie"
}

// METHOD 3
let dogName = {
    return "Charlie"
}

// METHOD 4
var dogName: String = {
    return "Charlie"
}()

Obviously the method 3 declare a let and we known the difference; but why Swift allows the method 4?

What's the difference between these four methods?

I'm quite confusing in particular between method 2 and 4. In addition why the method 3 lose the final brackets compared to method 4?

like image 834
Massimo Polimeni Avatar asked Jul 12 '17 13:07

Massimo Polimeni


People also ask

What is the proper way to declare a variable?

To declare (create) a variable, you will specify the type, leave at least one space, then the name for the variable and end the line with a semicolon ( ; ). Java uses the keyword int for integer, double for a floating point number (a double precision number), and boolean for a Boolean value (true or false).

How do you declare an int variable in Swift?

Declaring an int variable In Swift, the “var” keyword is used to declare a variable and you don't have to specify the type if it can be inferred from what you're assigning it. Also notice in Swift, we're using the String class as opposed to NSString.

How do I declare a variable in Swift 4?

Type Annotations. The following example shows how to declare a variable in Swift 4 using Annotation. Here it is important to note that if we are not using type annotation, then it becomes mandatory to provide an initial value for the variable, otherwise we can just declare our variable using type annotation.


1 Answers

Method 1 is a standard variable declaration for a String. It has a setter and a getter

var dogName: String = "Charlie"

print(dogName) -> "Charlie"
dogName = "Rex" // Valid

Method 2 is a computed property of type String and is read-only

var dogName: String {
    return "Charlie"
}

print(dogName) -> "Charlie"
dogName = "Rex" // Invalid as property is read-only

Method 3 is a read-only property of type () -> String, so basically a lambda function.

let dogName = {
    return "Charlie"
}

print(dogName) -> "(Function)"
print(dogName()) -> "Charlie"
dogName = "Rex" // Invalid as property is read-only

Method 4 is a closure that will be executed when the containing object is initialised. As it is a var you can replace it with another value

var dogName: String = {
    return "Charlie"
}()

print(dogName) -> "Charlie"
dogName = "Rex" // Valid

That being said, as Method 4 is a closure, you can execute other commands in it. Here is an example where you could use this construct to initialise a UILabel:

var dogNameLabel: UILabel = {
    let label = UILabel(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
    label.text = "Charlie"
    return label
}()
like image 186
Thomas Avatar answered Sep 28 '22 17:09

Thomas