Swift: Declaring empty tuples Swift: Declaring empty tuples ios ios

Swift: Declaring empty tuples


There's no such thing as an "unfilled" tuple value. In other words you don't create an empty tuple and then add values to it later. It's important to remember that tuples aren't collections like Array or Dictionary. Tuples are structured types. For example, you can't iterate through a tuple with a for loop. In your example, myTuple is a single value that happens to contain a String and an Int.

A tuple is like an on-demand unnamed structure, such as the following struct but if it were possible for it to be unnamed:

struct MyStruct {    let key: String    let val: Int}

If you want to model a missing tuple value, you should make the type of the entire tuple optional. For example:

var myTuple: (key: String, val: Int)? = nil