Swift Language Functional Programming in Swift Extracting a list of names from a list of Person(s)

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Given a Person struct

struct Person {
    let name: String
    let birthYear: Int?
}

and an Array of Person(s)

let persons = [
    Person(name: "Walter White", birthYear: 1959),
    Person(name: "Jesse Pinkman", birthYear: 1984),
    Person(name: "Skyler White", birthYear: 1970),
    Person(name: "Saul Goodman", birthYear: nil)
]

we can retrieve an array of String containing the name property of each Person.

let names = persons.map { $0.name }
// ["Walter White", "Jesse Pinkman", "Skyler White", "Saul Goodman"]


Got any Swift Language Question?