Skip to content

Commit 460ac45

Browse files
committed
Merge pull request #217 from hartbit/property-selectors
Final writeup of the property selector proposal
2 parents 2b70665 + 91ef556 commit 460ac45

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

proposals/XXXX-property-selectors.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Referencing the Objective-C selector of property getters and setters
2+
3+
* Proposal: SE-XXXX
4+
* Author(s): [David Hart](https://github.com/hartbit)
5+
* Status: TBD
6+
* Review manager: TBD
7+
8+
## Introduction
9+
10+
Proposal [SE-0022](https://github.com/apple/swift-evolution/blob/master/proposals/0022-objc-selectors.md) 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.
11+
12+
[Original swift-evolution thread](http://article.gmane.org/gmane.comp.lang.swift.evolution/7614)
13+
[Follow-up swift-evolution thread](http://thread.gmane.org/gmane.comp.lang.swift.evolution/7780)
14+
15+
## Motivation
16+
17+
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.
18+
19+
## Proposed solution
20+
21+
Introduce two new overrides to the `#selector` expression that allows building a selector which points to the getter or the setter of a property.
22+
23+
```swift
24+
class Person: NSObject {
25+
dynamic var firstName: String
26+
dynamic let lastName: String
27+
dynamic var fullName: String {
28+
return "\(firstName) \(lastName)"
29+
}
30+
31+
init(firstName: String, lastName: String) {
32+
self.firstName = firstName
33+
self.lastName = lastName
34+
}
35+
}
36+
37+
let firstNameGetter = #selector(getter: Person.firstName)
38+
let firstNameSetter = #selector(setter: Person.firstName)
39+
```
40+
41+
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.
42+
43+
```
44+
let lastNameSetter = #selector(setter: Person.lastName)
45+
// Argument of #selector(setter:) must refer to a variable property
46+
```
47+
48+
## Impact on existing code
49+
50+
The introduction of the new `#selector` overrides has no impact on existing code and could improve the string-literal-as-selector to `#selector` migrator.
51+
52+
## Alternatives considered
53+
54+
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.
55+

0 commit comments

Comments
 (0)