Tuples in Swift occupy the space between dictionaries and structures: they hold very specific types of data (like a struct) but can be created on the fly (like dictionaries). They are commonly used to return multiple values from a function call.
You can create a basic tuple like this:
1 |
let person = (name: "Paul", age: 35) |
1 2 3 4 5 6 7 8 9 10 11 12 |
func split(name: String) -> (firstName: String, lastName: String) { let split = name.components(separatedBy: " ") return (split[0], split[1]) // use tuple like this } let parts = split(name: "Paul Hudson") // used like this parts.0 parts.1 parts.firstName parts.lastName |