Ans :
Optional : Optional is swift's powerful feature which come to solve problem of non-existing value. It is just a type . There are two types as Int (which must has a value) and Int?(which may contain a Int value or may have nil value). It is declared as T? i.e Int?, String?
Forced Unwrapping : Exclamation mark ( ! ) is used to unwrap value.
i.e let optionalInt : Int? = 5
let intVal : Int = 2
optionalInt! + intVal
So we hvae forecfully unwrap optionaInt. That means we tell compiler that optionalInt has a value and extract and use it.
But this is not good practise. If sometimes optionaInt has not value and we try to unwrap, then app will be crashed. A good practise is to check with nil before unwrapping or use optional binding. It checks it has value or not and if and only if it has value extract it and use it.
Optional Binding : You use optional binding to check if the optional contains a value or not. If it does contain a value, unwrap it and put it into a temporary constant or variable.
Example :
var stockCode:String? = findStockCode("Facebook")
let text = "Stock Code - "
if let tempStockCode = stockCode {
let message = text + tempStockCode
println(message)
}
The “if let” (or “if var”) are the two keywords of optional binding. In plain English, the code says “If stockCode contains a value, unwrap it, set its value to tempStockCode and execute the conditional block. Otherwise, just skip it the block”. As the tempStockCode is a new constant, you no longer need to use the ! suffix to access its value.
Implicitly Unwrapped Optional : When we are very very sure about it has value after first time it is set, then we need not unwrap every time. So for this type of scenario, we have to use it with ! mark in their type.
// forced unwrapping
let optionalInt: Int? = 123
let forcedInt: Int = optionalInt!
// implicitly unwrapped optional
let assumedInt: Int! = 123
let implicitInt: Int = assumedInt
It may has nil value.
Optional Chaining :
The feature allows us to chain multiple optionals together with the “?.” operator.
if let sharePrice = findStockCode("Apple")?.price {
let totalCost = sharePrice * 100
println(totalCost)
}
FindstockCode method returns optional value. We can access multiple optional together using Optional chaining feature.
Optional : Optional is swift's powerful feature which come to solve problem of non-existing value. It is just a type . There are two types as Int (which must has a value) and Int?(which may contain a Int value or may have nil value). It is declared as T? i.e Int?, String?
Forced Unwrapping : Exclamation mark ( ! ) is used to unwrap value.
i.e let optionalInt : Int? = 5
let intVal : Int = 2
optionalInt! + intVal
So we hvae forecfully unwrap optionaInt. That means we tell compiler that optionalInt has a value and extract and use it.
But this is not good practise. If sometimes optionaInt has not value and we try to unwrap, then app will be crashed. A good practise is to check with nil before unwrapping or use optional binding. It checks it has value or not and if and only if it has value extract it and use it.
Optional Binding : You use optional binding to check if the optional contains a value or not. If it does contain a value, unwrap it and put it into a temporary constant or variable.
Example :
var stockCode:String? = findStockCode("Facebook")
let text = "Stock Code - "
if let tempStockCode = stockCode {
let message = text + tempStockCode
println(message)
}
The “if let” (or “if var”) are the two keywords of optional binding. In plain English, the code says “If stockCode contains a value, unwrap it, set its value to tempStockCode and execute the conditional block. Otherwise, just skip it the block”. As the tempStockCode is a new constant, you no longer need to use the ! suffix to access its value.
Implicitly Unwrapped Optional : When we are very very sure about it has value after first time it is set, then we need not unwrap every time. So for this type of scenario, we have to use it with ! mark in their type.
// forced unwrapping
let optionalInt: Int? = 123
let forcedInt: Int = optionalInt!
// implicitly unwrapped optional
let assumedInt: Int! = 123
let implicitInt: Int = assumedInt
It may has nil value.
Optional Chaining :
The feature allows us to chain multiple optionals together with the “?.” operator.
if let sharePrice = findStockCode("Apple")?.price {
let totalCost = sharePrice * 100
println(totalCost)
}
FindstockCode method returns optional value. We can access multiple optional together using Optional chaining feature.
Optional Chaining
ReplyDeleteOptional chaining is the way by which we try to retrieve a values from a chain of optional values. Let us take the following example classes.
class School {
var director:Person? }
class Person {
var name: String = "" init(name: String) {
self.name = name
}
}
var school = School()
var person = Person(name: "Jason") school.director = person school.director?.name
The director property in School class is optional, when you try to access subsequent values from director property becomes optional (? mark after director when accessing name property). You can handle these optionals as shown below.
if let name = school.director?.name { println("Director name is \(name)")
} else{
println("Director yet to be assigned")
}
Optional chaining is a process of querying and calling properties. Multiple queries can be chained together, and if any link in the chain is nil then, the entire chain fails.
Nice explaination.... Thank You
ReplyDelete