Swift Optional Chaining - Swift Programming

What is Swift - Optional Chaining?

The processing of querying, calling properties, subscripts and methods on the optional that may be 'nil' can be defined as optional chaining. Optional chaining return two values -

  • if optional contains a 'value' then calling its related property, methods and subscripts returns values.
  • if optional contains a 'nil' value all its its related property, methods and subscripts returns nil.

Since there are multiple queries to methods, properties and subscripts are grouped together and a failure to one chain will affect the entire chain and results in a 'nil' value.

Optional Chaining as an Alternative to Forced Unwrapping

Optional chaining is generally specified after the optional value with '?' to call a property, method or subscript when the optional value returns some values.

Optional Chaining '?'

Access to methods,properties and subscriptsOptional Chaining '!' to force Unwrapping

? is placed after the optional value to call property, method or subscript

! is placed after the optional value to call property, method or subscript to force unwrapping of value

Fails gracefully when the optional is 'nil'

Forced unwrapping triggers a run time error when the optional is 'nil'

When we run the above program using playground, we get the following result-

The above program declares 'election poll' with a class name and contains 'candidate' as membership function. The subclass is then declared as the 'poll booth' and the 'name' as its membership function which is initialized as 'MP'. The call to the super class is then initialized by the creation of an instance 'cand' with optional '!'. Since the values are not declared in its base class,the 'nil' value is stored thereby returning a fatal error by the force unwrapping procedure.

Program for Optional Chaining with '?'

When we run the above program using playground, we get the following result -

The program above declares the 'election poll' with a class name and contains the 'candidate' as membership function. The subclass is then declared as 'poll booth' and the 'name' as its membership function which is initialized as 'MP'. The call to the super class is now initialized by creating an instance 'cand' with optional '?'. Since the values are not declared in its base class 'nil' value it is stored and printed in the console by thehelp of the else handler block.

Defining Model Classes for Optional Chaining & Accessing Properties

Swift language also provides us with the concept of optional chaining, and it allows us to declare more than one subclasses as its model classes. This concept is very useful to define the complex models and to access the properties, methods and subscripts sub properties.

When we run the above program using playground, we get the following result -

Calling Methods Through Optional Chaining

When we run the above program using playground, we get the following result -

The function circleprint() then declares the inside of the circle() sub class by creating an instance named 'circname'. The function will return us a value if it contains some value otherwise it will return some user defined print message by having a check at the statement 'if circname.print?.circleprint() != nil'.

Accessing Subscripts through Optional Chaining

Optional chaining can be used to set and retrieve a subscript value to validate whether the call to that subscript returns a value. '?' is generally placed before the subscript braces to access the optional value on particular subscript.

Program 1

When we run the above program using playground, we get the following result -

From the above program the instance values of the membership function 'radiusName' is not specified. Hence the program call to the function will only return else part whereas to return the values we need to define the values for the particular membership function.

Program 2

When we run the above program using playground, we get the following result -

From the above program, the instance values for the membership function 'radiusName'have been specified. Hence the program call to the function will now return values.

Accessing Subscripts of Optional Type

When we run the above program using playground, we get the following result -

The optional values for the subscripts can be accessed by referring to their subscript values. It can also be accessed as subscript[0], subscript[1] etc. The default subscript values for the 'radius' are first assigned as [35, 45, 78, 101] and for the 'Circle' [90, 45, 56]]. Then the subscript values are changed as Radius[0] to 78 and then Circle[1] to 45.

Linking Multiple Levels of Chaining

Multiple sub classes can also be linked with its super class methods, properties and subscripts by optional chaining.

Multiple chaining of optional can also be linked -

If the retrieving type is not optional,then optional chaining will return as an optional value. For example if String through optional chaining it will return String? Value

When we run the above program using playground, we get the following result -

In the above program, the instance values of the membership function 'radiusName' is not specified. Hence, the program call to the function will return only else part whereas if the values are to be returned we have to define the values for the particular membership function.

If the retrieving type is also already optional, then optional chaining will also return an optional value. For example if String? Is accessed through optional chaining it will return String? Value.

When we run the above program using playground, we get the following result -

In the above program, the instance values for the membership function 'radiusName' is specified. Therefore, the program call to the function will now return values.

  • Chaining on Methods with Optional Return Values
  • Optional chaining is used to access subclasses defined methods too.

When we run the above program using playground, we get the following result -

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Swift Programming Topics