Objective-C has the ability to refer to a property dynamically, and not directly. These links are called key. They differ from direct access to properties because they actually do not read or write the value, they just hid it for use.
Define a structure called Cavaliers and a structure called Player, then create one instance of each of them:
struct Player {
var name: String
var rank: String
}
struct Cavaliers {
var name: String
var maxPoint: Double
var captain: Player
func goTomaxPoint() {
print("\(name) is now travelling at warp \(maxPoint)")
}
}
let james = Player(name: "Lebron", rank: "Captain")
let irving = Cavaliers(name: "Kyrie", maxPoint: 9.975, captain: james)
let score = irving.goTomaxPoint
score()
goTomaxPoint(), . , , keypath .
let nameKeyPath = \Cavaliers.name
let maxPointKeyPath = \Cavaliers.maxPoint
let captainName = \Cavaliers.captain.name
let cavaliersName = irving[keyPath: nameKeyPath]
let cavaliersMaxPoint = irving[keyPath: maxPointKeyPath]
let cavaliersNameCaptain = irving[keyPath: captainName]
, Xcode9 .