- Proposal: SE-0064
- Author: David Hart
- Status: Implemented in Swift 3
- Review manager: Doug Gregor
Proposal SE-0022 was accepted and implemented to provide a #selector
expression to reference Objective-C method selectors. Unfortunately, it does not allow referencing the getter and setter methods of properties. This proposal seeks to provide a design to reference those methods for the Swift 3.0 timeframe.
Original swift-evolution thread Follow-up swift-evolution thread
The #selector
expression is very useful but does not yet cover all cases. Accessing property getter and setters requires to drop down to the string syntax and forgo type-safety. This proposal supports this special case without introducing new syntax, but by introducing new overloads to the #selector
compiler expression.
Introduce two new overrides to the #selector
expression that allows building a selector which points to the getter or the setter of a property.
class Person: NSObject {
dynamic var firstName: String
dynamic let lastName: String
dynamic var fullName: String {
return "\(firstName) \(lastName)"
}
init(firstName: String, lastName: String) {
self.firstName = firstName
self.lastName = lastName
}
}
let firstNameGetter = #selector(getter: Person.firstName)
let firstNameSetter = #selector(setter: Person.firstName)
Both overrides expect a property and the setter requires a variable property. For example, the following line of code would produce an error because the lastName property is defined with let.
let lastNameSetter = #selector(setter: Person.lastName)
// Argument of #selector(setter:) must refer to a variable property
The introduction of the new #selector
overrides has no impact on existing code and could improve the string-literal-as-selector to #selector
migrator.
A long term alternive could arrise from the design of lenses in Swift. But as this is purely hypothetical and out of scope for Swift 3, this proposal fixes the need for referencing property selectors in a type-safe way straight-away.