class User: NSObject { // setValue 는 NSObject 의 method
@objc var login: String = "raw" // string -> key 로 접근 가능
}
func hasProperty(_ name: String, object: AnyObject) -> Bool {
let mirror = Mirror(reflecting: object)
for (label, _) in mirror.children {
if label == name { return true }
}
return false
}
var user = User() // login: "raw"
let parameters: [String: Any] = ["login": "octodog"]
for (key, value) in parameters {
guard hasProperty(key, object: user) else { continue }
user.setValue(value, forKey: key)
}
print(user.login) // login: "octodog"