Home > AI > Language > Swift >

@dynamicMemberLookup

Example 1:

@dynamicMemberLookup
struct Person {
    subscript(dynamicMember member: String) -> String {
        let properties = ["name": "Taylor Swift", "city": "Nashville"]
        return properties[member, default: ""]
    }
}

let taylor = Person()
print(taylor.name)
print(taylor.city)
print(taylor.favoriteIceCream)

Leave a Reply