-
Book Overview & Buying
-
Table Of Contents
Mastering Swift 6 - Seventh Edition
By :
Dynamic member lookup enables a call to a property that will be dynamically resolved at runtime. This may not make a lot of sense without seeing an example, so let's create one.
Let's say that we had a structure that represented a baseball team. This structure has a property that represents the city the team is from and another property that represents the nickname of the team. The following code shows this structure:
struct BaseballTeam {
let city: String
let nickName: String
}
In this structure, if we needed a way to retrieve the full name of the baseball team, including the city and nickname, we would probably create a method as shown in the following example:
func fullname() -> String {
return "\(city) \(nickName)"
}
This is how it would be done in most object-oriented programming languages. In our code which uses the BaseballTeam structure, we could then retrieve the city and nickname as properties with the dot notation...