Saturday, March 28, 2015

Tuples

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.

SwiftUI Search & Filter with Combine - Part 3 (iOS, Xcode 13, SwiftUI, 2...

In part 3 of the Searchable video series, I show you how to use Combine in #SwiftUI for the search and filter logic connected to the searcha...