Optional Chaining

When you have optional methods that may or may not exist, you need to be able to call them without the possibility of crashing your program. Did you think that the optional methods in protocols are just optional, meaning that you can include them or not include them? If you did, you were wrong. They are directly tied to optionals and can be checked via value binding.

Using optional chaining is another possibility instead of forcing the unwrapping of optionals. The big difference between optional chaining and forced unwrapping is that whereas forced unwrapping gives you an error and crashes your program if the thing you are looking for does not exist, optional chaining does not.

When you have an optional method that may or may ...

Get Learning Swift™ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.