Optional types with tuples

We can define a whole tuple as an optional or any of the elements within a tuple as an optional. It is especially useful to use optionals with tuples when we return a tuple from a function or method. This allows us to return part (or all) of the tuple as nil. The following example shows how to define a tuple as an optional, and also how to define individual elements of a tuple as optional types:

var tuple1: (one: String, two: Int)?  
var tuple2: (one: String, two: Int?) 

The first line defines the whole tuple as an optional type. The second line defines the second value within the tuple as an optional, while the first value is a non-optional.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset