Swift: Optional variable

This entry is part 35 of 41 in the series Swift

For Swift, when you declare a variable, you need to provide an initial value.  If the value is unknown, you can declare it as an optional.

var name:String?

//name = "abcde"

if name != nil {
    print("Your name: \(name!)")
} else {
    print("There is no value in the Name")


Series Navigation<< Swift: The 2 methods of writing a closure
Swift: Initialising parameters in a struct >>

Leave a comment

Leave a Reply