Swift

Collections

Higher Order Functions

Given the following

struct Person {
    let firstname: String
    let lastname: String
    let age: Int
}

let personArray = [
    Person(firstname: "Rynaard", lastname: "Burger", age: 36),
    Person(firstname: "Foo", lastname: "Bar", age: 24)
]

map

Apply transformations to items in the collection.

let uppercasedFirstnames = personArray.map { $0.firstname.uppercased() }

compactMap (replaced flatMap in Swift 4.1)

Transform each item in the collection and remove any items which are nil.

filter

Filters out items in a collection.

reduce

Reduces the collection down to a single result.

sort

Sort by optional date.

Dictionaries

Get all the keys

Get all the values

Last updated