How to Read a Variable Statement Swift
Swift - Optionals
Swift 4 also introduces Optionals type, which handles the absence of a value. Optionals say either "at that place is a value, and it equals x" or "in that location isn't a value at all".
An Optional is a type on its own, actually one of Swift four'south new super-powered enums. It has two possible values, None and Some(T), where T is an associated value of the right information blazon available in Swift 4.
Here's an optional Integer proclamation −
var perhapsInt: Int?
Hither's an optional Cord announcement −
var perhapsStr: String?
The higher up proclamation is equivalent to explicitly initializing it to nil which means no value −
var perhapsStr: Cord? = nil
Let's have the post-obit instance to understand how optionals work in Swift iv −
var myString:String? = aught if myString != cipher { print(myString) } else { print("myString has zippo value") }
When we run the in a higher place plan using playground, we go the following result −
myString has cipher value
Optionals are like to using nil with pointers in Objective-C, but they work for any type, not only classes.
Forced Unwrapping
If you defined a variable as optional, then to go the value from this variable, you will accept to unwrap it. This just means putting an exclamation marking at the stop of the variable.
Permit'south have a unproblematic instance −
var myString:Cord? myString = "Howdy, Swift four!" if myString != nil { print(myString) } else { impress("myString has null value") }
When nosotros run the in a higher place program using playground, we get the following event −
Optional("How-do-you-do, Swift 4!")
At present allow's utilize unwrapping to go the correct value of the variable −
var myString:String? myString = "Hello, Swift 4!" if myString != nil { print( myString! ) } else { print("myString has nada value") }
When we run the above programme using playground, we get the following result.
Hello, Swift iv!
Automatic Unwrapping
You lot tin declare optional variables using exclamation mark instead of a question mark. Such optional variables will unwrap automatically and y'all do not need to use any further exclamation mark at the end of the variable to get the assigned value. Let's take a simple example −
var myString:String! myString = "Hello, Swift four!" if myString != naught { print(myString) } else { print("myString has cypher value") }
When we run the above programme using playground, we become the following event −
Hello, Swift 4!
Optional Binding
Use optional bounden to discover out whether an optional contains a value, and if so, to brand that value available equally a temporary constant or variable.
An optional binding for the if statement is as follows −
if allow constantName = someOptional { statements }
Let's accept a simple instance to empathize the usage of optional binding −
var myString:String? myString = "Hello, Swift 4!" if permit yourString = myString { print("Your cord has - \(yourString)") } else { print("Your cord does not have a value") }
When we run the to a higher place plan using playground, nosotros get the post-obit effect −
Your string has - How-do-you-do, Swift iv!
Useful Video Courses
Video
Video
Video
Video
Video
Video
Source: https://www.tutorialspoint.com/swift/swift_optionals.htm