What is tuples?

One of the advanced features in Swift is the feature called Tuples in which you could create and pass around groupings of value. And with tuple, you could return multiple values from a function as a single compound value.

The value within a tuple can be of any type and don’t have to be of the same type as each other. An example of grouping multiple values into a single compound value. In this case, this is called “tuple of type (Int, String)”.

There are definitely many different way to play around with tuple such as creating a tuple of type (Int, Int, Int), or (String, Bool).

If you would like to extract a single information into separate constants or variable, it could be done in this way.

The alternative way to extract single information could be done differently. However, the result is totally the same as above.

How would tuples be useful in helping you develop iOS Apps?

Tuples could be very useful as return value of functions in most cases when the function tries to retrieve web page that could possible return (Int, String) to describe the success or failure of the loading of the page. With the information of two distinct value, it could provide more useful information about the outcome rather than only returning a single value of a type.

Also, tuples are not recommended for the creation of complex data structure. But are more recommended for temporary groups of related values.

  • Article By :
    Founder of DaddyCoding. Studied Computer Science, Information System and Information Technology at BYU-Hawaii. Currently spending most of my time researching and learning on helping to expose making iOS apps.

Random Posts

Leave a Reply

Your email address will not be published. Required fields are marked *