Key-path expressions as functions
SE-0249 in Swift 5.2 introduced a great shortcut which enables us to easily access the properties of objects in a particular collection. What this means is if we iterate over a collection using the map
algorithm, we are able to use key-path expressions (\Root.value
) to access the properties of the items in the collection. Let's look at an example using the employee structure we created earlier. We will start by creating three employees and adding them to an array:
let employee1 = EmployeeStruct(firstName: "Jon", lastName: "Hoffman", salaryYear: 90000)
let employee2 = EmployeeStruct(firstName: "Kailey", lastName: "Hoffman", salaryYear: 32000)
let employee3 = EmployeeStruct(firstName: "Kara", lastName: "Hoffman", salaryYear: 28000)
let employeeCollection = [employee1, employee2, employee3]
Now that we have an array of employees, let's retrieve all of the first names of our...