Questions 46 to 50 in 50
Page 8
If
weak
doesn’t retain the source object, why unowned
exist ? Weak vs Unowned ?
Both Weak and Unowned shall not increase retain count.
Both of them creates Setter and Getter methods respectively.
Upon deallocation the object will be set to nil for weak but not for unowned.
Weak references are always optional whereas unowned references won’t be optional.
Since Unowned is not optional reference and not set to nil, we should use only when we really know object once assigned will never be nil.
For example if we are setting a singleton instance to another object so it could access it for its lifetime, we should use unowned. using weak in this case shall risk singleton deallocation or memory leak depending on where the first singleton instance is created.
Both of them creates Setter and Getter methods respectively.
Upon deallocation the object will be set to nil for weak but not for unowned.
Weak references are always optional whereas unowned references won’t be optional.
Since Unowned is not optional reference and not set to nil, we should use only when we really know object once assigned will never be nil.
For example if we are setting a singleton instance to another object so it could access it for its lifetime, we should use unowned. using weak in this case shall risk singleton deallocation or memory leak depending on where the first singleton instance is created.
What are optionals and how they are important?
Optional means a reference or scope instance shall be nil or can have value.
They are important as they allow us do following
They are important as they allow us do following
- They allow us to write more safe and flexible code.
- They allow a function to return multiple returns in multiple scenarios by using optional out parameters
- They allow to create local scope copies only when they have values so more nil checks can be avoided (If we have 4 optionals its easier to write 4 guard statements instead of writing if nil checks at all occurences of those 4 variables)
what is
??
(Double Question Mark) in swift?
As swift allows creation of optionals that may have nil or an actual value, to allow nil checks prior to unwrapping them (force unwraping
especially) we can use
In other words its a short hand for terinary operator
It is widely used in giving default values when no value(nil) exist.
??
In other words its a short hand for terinary operator
?:
where condition is only nil check.
- Without
??
A!=NIL ? A! : B
- With
??
A ?? B
It is widely used in giving default values when no value(nil) exist.
What is Optional Chaining ?
Optional chaining is a process for querying and calling properties, methods, and subscripts on an optional that might currently be nil.
The call succeeds to property, method, or subscript only if the optional contains a value
Optional Chaining is a safe alternative to Forced Unwrapping. usage of
Optional Chaining can be used in nested reference level as well. Below is example where a nested call is being made on person instance to fetch street using chaining
In simple words Optional chaining is a methodology which can allow the code to fail gracefuly. Anything after script is called only when the script executed till that level results in non null value. like in above example address is fetched when only residence exists and street is fetched only when address exists.
The call succeeds to property, method, or subscript only if the optional contains a value
Optional chaining in Swift is similar to messaging nil in Objective-C, but in a way that works for any type, and that can be checked for success or failure.
Optional Chaining is a safe alternative to Forced Unwrapping. usage of
?
is much more encourged than using !
.Optional Chaining can be used in nested reference level as well. Below is example where a nested call is being made on person instance to fetch street using chaining
if let personsStreet = person.residence?.address?.street { print("person's street name is \(personsStreet).") } else { print("Unable to retrieve the address.") }
In simple words Optional chaining is a methodology which can allow the code to fail gracefuly. Anything after script is called only when the script executed till that level results in non null value. like in above example address is fetched when only residence exists and street is fetched only when address exists.
What is Optional Binding? How it differs from Optional Chaining?
Binding an opional into a temporary local scope variable only after deciding whether the optional has a value in it is called Optional Binding.
This means all the ways of unwrapping an optional except Force Unwrapping comes under Optional Binding.
In below example all person would have first and last names whereas only some people would have middle name, this would be binded as following.
Optional Binding is a procedure of handling graceful exits after optional chaining, So Optional Binding is Control statement where as Optional Chaining is Decision statement.
This means all the ways of unwrapping an optional except Force Unwrapping comes under Optional Binding.
In below example all person would have first and last names whereas only some people would have middle name, this would be binded as following.
if let middlename = person.middlename { print("person's middle name is \(middlename).") } else { print("Unable to retrieve the middlename.") } // (Or) function middlename() -> String { guard let mname = person.middlenames else { show("No middle name to return") return "" } return mname; }
Optional Binding is a procedure of handling graceful exits after optional chaining, So Optional Binding is Control statement where as Optional Chaining is Decision statement.