Reputation: 10139
In Javascript I can reference properties of an object like this:
obj.prop
Or like this:
obj["prop"]
Is there a similar method in Swift of accessing the properties of an object with a string?
Upvotes: 1
Views: 67
Reputation: 59994
There is no way to do it that would work for any Swift class. However, you can do this:
class User: NSObject {
dynamic var username: String = "admin"
dynamic var password: String = "123456"
}
let u = User()
u.valueForKey("username") // admin
This works if:
NSObject
protocol;var
s as dynamic
.Upvotes: 2