Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Founder. Opencart
Top Partners
Optional Chaining in swift is the process in which we can query or call the methods, properties or subscripts of an optional that might be nil. Generally, in swift optional chaining will return a value in two ways.
nil
value
In swift, multiple queries can be chained together due to that if any link in the chain fails or return a nil value, the entire chain will fail and return a nil value.
You specify optional chaining by placing a question mark (?) after the optional value on which you wish to call a property, method or subscript if the optional is not-nil. This is very similar to placing an exclamation mark (!) after an optional value to force the unwrapping of its value. The main difference is that optional chaining fails gracefully when the optional is nil, whereas forced unwrapping triggers a runtime error when the optional is nil.
?
!
The result of optional chaining will be of the same type as expected return value with wrapped in an optional. A property that normally returns an String will return a String?when accessed through optional chaining.
String
String?
The below code snippets demonstrate how optional chaining differs from forced unwrapping and enables you to check for success.
First, two classes called Company and Address are defined:
Company
Address
Address instances have the optional property like street, postcode, city, country. Company instances have an optional address property of type Address?.
address
Address?
If you create a new Company instance, its address property is default initialized to nil, by virtue of being optional. In the code below, webkul has a address property value of nil:
webkul
If you try to access the country property or any other property of this company’s address, by placing an exclamation mark after address to force the unwrapping of its value, you trigger a runtime error, because there is no address value to unwrap:
country
The code above succeeds when webkul.address has a non-nil value and will set country to an String value containing the appropriate country. However, this code always triggers a runtime error when address is nil, as illustrated above.
webkul.address
non-nil
Optional chaining provides an alternative way to access the value of country. To use optional chaining, use a question mark in place of the exclamation mark:
This tells Swift to “chain” on the optional address property and to retrieve the value of country if address exists.
Because of the attempt to access the potential to fail, the optional chaining returns a value of type String?, or “optional String”. When address is nil, as in the example above, this optional String will also be nil, to reflect the fact that it was not possible to access country. The optional String is accessed through optional binding to unwrap the string and assign the non-optional value to the country variable.
Note that this is true even though country is a non-optional String. The fact that it is queried through an optional chain means that the call to country will always return an String? instead of an String.
You can assign a address instance to webkul.address, so that it no longer has a nilvalue:
webkul.address now contains an actual Address instance, rather than nil. If you try to access country with the same optional chaining as before, it will now return an String?that contains the country value as India:
India
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
Excellent work, fast, good quality and understood the brief perfectly! Quick responses developing the project and very good cooperation. I suggest to anyone.
Stathis Plakidas
USA
Global
Name
Email
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.