Start a Project

Optional Chaining in swift

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.

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.

Optional Chaining as an Alternative to Forced Unwrapping

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.

Example:

The below code snippets demonstrate how optional chaining differs from forced unwrapping and enables you to check for success.

Defining models and structure:

First, two classes called Company and Address are defined:

Address instances have the optional property like street, postcode, city, country. Company instances have an optional address property of type 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:

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:

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.

Uses:

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:

Thank You!!!

Exit mobile version