[swift-evolution] Argument of '#selector' cannot refer to a property?
David Hart
david at hartbit.com
Sun Feb 21 03:19:47 CST 2016
I’ve played with the new #selector syntax and am loving it. But I often want to use it as a way to provide strong-typing to APIs which require String representations of properties. For example:
class Model: NSObject {
dynamic var firstName: String = ""
dynamic var lastName: String = ""
}
extension Model {
var jsonValue: [String:AnyObject] {
return [
#selector(self.firstName): firstName,
#selector(self.lastName): lastName
]
}
}
extension Model {
func modelWithName(name: String) {
return Database.execute("SELECT * FROM Model WHERE \(#selector(self.firstName)) = ? OR \(#selector(self.lastName)) = ?")
}
}
But there are currently two problems with this:
1) The compiler doesn’t accept #selector of properties with the error message: Argument of '#selector' cannot refer to a property. What’s the reasoning for that?
2) Even if it did, I would still have the wrap #selector with NSStringFromSelector. It would be really convenient if Selector objects would convert to String. How is that possible?
More information about the swift-evolution
mailing list