Declaring
// Combine any values/types together into one variable/constant
let myNameTuple = ("Mark", "Moeykens", 44)
// Values in a tuple are called "elements" or "element values"
Access elements
// By Index
print("My first name is \(myNameTuple.0)")
// By naming the elements
let (myFirstName, myLastName, myAge) = myNameTuple
print("My first name is \(myFirstName)")
// Ignore elements with underscore (_)
let (justFirstName, _, _) = myNameTuple
print("My first name is \(justFirstName)")
// You can name the elements in a tuple too
let personTuple = (firstName: "Mark", lastName: "Moeykens", age: 44)
// Accessing elements in a tuple
var firstName = personTuple.firstName
(Updated for Swift 2.2)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.