Key Paths
Introduction
Welcome to the second lesson of What’s New in Swift 4. You will discover how to save time by typing less. No need to talk much. Let’s get started.
Problem
I’m tired of chaining.
Design Model
struct Developer {
var platform: Platform
var information: Information
}
enum Platform {
case iOS
case android
}
struct Information {
var name, strengths, motivation: String
}
Create Objects
let robInformation = Information(name: "Rob", strengths: "Zenness", motivation: "Change the world")
let bobInformation = Information(name: "Bob", strengths: "Dryness", motivation: "None")
let rob = Developer(platform: .android, information: robInformation)
let bob = Developer(platform: .iOS, information: bobInformation)
Your Past
In order to access the name
property, you had to chain through for each object.
rob.information.name // "Rob"
bob.information.name // "Bob"
However, in Swift 4, it provides an alternative that provides safety and less typing.
Introduction Swift 4 Key Paths
You may have the access/path to the property or method as shown below.
let bobPlatform = bob[keyPath: \Developer.platform] // iOS
let bobName = bob[keyPath: \Developer.information.name] // 'Bob"
Store Path
You may store the path and simply apply.
let informationKeyPath = \Developer.information
let bobInfo = bob[keyPath: informationKeyPath]
let robInfo = rob[keyPath: informationKeyPath]
Append Key Path
Fortunately, you may also append more paths to the original path as long as there is one.
let nameKeyPath = informationKeyPath.appending(path: \.name)
bob[keyPath: nameKeyPath] // "Bob"
rob[keyPath: nameKeyPath] // "Rob"
Source Code
Resources
Smart KeyPaths: Better Key-Value Coding for Swift - Apple
Conclusion
Congratulations. Remember, even if you are chaining through, you may make a single auto-completion mistake that leads to catastrophic result. Although it is a brand new feature, if you need to access Property or Method through many chainings, I recommend you to utilize the keypath API provided in Swift 4. If you want to learn much deeper, feel free to take a look at the Apple’s proposal documentation in the lecture notes.
In the following lesson, you will learn how to make subscripts generic, which isn’t that important but at least you can brag from what you’ve learned.