Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would I use if and let together, instead of just checking if the original variable is nil? (Swift)

Tags:

swift

In “The Swift Programming Language.” book, Apple mentions using if and let together when accessing an optional variable.

The book gives the following code for example:

var optionalString: String? = "Hello"
optionalString == nil

var optionalName: String? = "John Appleseed"
var greeting = "Hello!"
if let name = optionalName {
    greeting = "Hello, \(name)"
}

What is the advantage of using if let name = optionalName, rather than if optionalName != nil (and always referring to it as, optionalName)? Is there any difference, or is it simply convention?

like image 972
hetelek Avatar asked Jun 04 '14 23:06

hetelek


People also ask

Why we use if let in Swift?

In if let , the defined let variables are available within the scope of that if condition but not in else condition or even below that. In guard let , the defined let variables are not available in the else condition but after that, it's available throughout till the function ends or anything.

When to use if let and guard let in Swift?

This makes our code easier to read than if we tried to check a condition then run some code, then check another condition and run some different code. So, use if let if you just want to unwrap some optionals, but prefer guard let if you're specifically checking that conditions are correct before continuing.

How do I check if a variable is nil in Swift?

In Swift, if we define a variable to be an optional variable, in that case, this variable can have no value at all. If optional variable is assigned with nil , then this says that there is no value in this variable. To check if this variable is not nil, we can use Swift Inequality Operator != .

How does if let work in Swift?

The “if let” allows us to unwrap optional values safely only when there is a value, and if not, the code block will not run. Simply put, its focus is on the “true” condition when a value exists.


1 Answers

Because it also unwraps the optional value, so this code:

if let name = optionalName {
    greeting = "Hello, \(name)"
}

is equivalent to:

if optionalName != nil {
    let name:String = optionalName!
    greeting = "Hello, \(name)"
}

This language sugar is known as Optional Binding in Swift.

Optional Types

In Swift T and T? are not the same types, but the underlying value of an optional T? type can easily be realized by using the ! postfix operator, e.g:

let name:String = optionalName!

Which now can be used where a String is expected, e.g:

func greet(name:String) -> String {
    return "Hello, \(name)"
}

greet(name)

Although as its safe to do so, Swift does let you implicitly cast to an optional type:

let name = "World"
let optionalName: String? = name

func greet(optionalName:String?) -> String? {
    if optionalName != nil {
        return "Hello, \(optionalName)"
    }
    return nil
}

//Can call with either String or String?
greet(optionalName)
greet(name)
like image 82
mythz Avatar answered Nov 10 '22 09:11

mythz