April 2017
Intermediate to advanced
316 pages
9h 33m
English
Optional chaining is a process to query and call properties, methods, and subscripts on an optional that may currently be nil. Optional chaining in Swift is similar to messaging nil in Objective-C but in a way that works for any type and can be checked for success or failure.
The following example presents two different classes. One of the classes, Person, has a property of type of Optional (residence), which wraps the other class type Residence:
class Residence { var numberOfRooms = 1 } class Person { var residence: Residence? }
We will create an instance of the Person class, sangeeth:
let residence = Residence() residence.numberOfRooms = 5 let sangeeth = Person() sangeeth.residence = residence
To check for numberOfRooms ...
Read now
Unlock full access