Tuples in Swift
We can group different values into a single value. What can possibly go wrong?
Tuples in Swift allows you to store multiple values in a single variable. Since we a representing multiple values as a single variable, it only really makes sense if the constituent parts are tightly connected.
Difficulty: Beginner | Easy | Normal | Challenging
Prerequisites:
- Be able to produce a “Hello, World!” iOS application (guide HERE)
Terminology
Data Types: A representation of the tyoe of data that can be processed, for example Integer or String
Tuple: A way in Swift to allow you to store multiple values in a single variable.
Storing multiple values
Using a Struct
One possible solution to storing multiple values is to use a Struct
struct Person {
var name: String
var age: Int
}let steve = Person(name: "Steve", age: 22)
Now, as you can see this is quite long and verbose. That is not to say that there are not uses for creating a Class
or a Struct
to store this data — but like many things in programming this is a question of style…