[swift-evolution] Argument of '#selector' cannot refer to a property?

Jordan Rose jordan_rose at apple.com
Mon Feb 22 12:28:30 CST 2016


> On Feb 21, 2016, at 1:19, David Hart via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 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?

It's not obvious whether you want the selector of the getter or the selector of the setter. It is a hole, though. Doug, did you have any comments here?


> 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?

I'm not sure why this would make sense. Selectors are not strings. (However, they are CustomStringConvertible, so you can just use "String(…)" to convert them.)

Jordan


More information about the swift-evolution mailing list