Dynamic member lookup
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 look at 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 was 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 wanted to retrieve the full name of the baseball team, including the city
and nickname
, we could easily create a method as shown in the following example:
func fullname() -> String {
return "\(city) \(nickName)"
}
This is how you would do it in most object-oriented programming languages. However, in our code, which uses the BaseballTeam
structure, we would retrieve the city and nickname as properties with...