Skip to content

Commit fc53a95

Browse files
committed
Merge pull request #70 from ikesyo/associated-property-noescape
Add noescape attribute for `initial` and `placeholder` closures of associatedProperty
2 parents 9613324 + 605abc9 commit fc53a95

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

Source/Foundation/Association.swift

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,13 @@ public func associatedProperty(host: AnyObject, keyPath: StaticString) -> Mutabl
3333
///
3434
/// This can be used as an alternative to `DynamicProperty` for creating strongly typed
3535
/// bindings on Cocoa objects.
36-
///
37-
/// N.B. Ensure that `host` isn't strongly captured by `placeholder`, otherwise this will
38-
/// create a retain cycle with `host` causing it to never dealloc.
39-
public func associatedProperty<T: AnyObject>(host: AnyObject, keyPath: StaticString, placeholder: () -> T) -> MutableProperty<T> {
40-
let initial: AnyObject -> T = { host in
41-
host.valueForKeyPath(keyPath.stringValue) as? T ?? placeholder()
42-
}
36+
public func associatedProperty<T: AnyObject>(host: AnyObject, keyPath: StaticString, @noescape placeholder: () -> T) -> MutableProperty<T> {
4337
let setter: (AnyObject, T) -> () = { host, newValue in
4438
host.setValue(newValue, forKeyPath: keyPath.stringValue)
4539
}
46-
return associatedProperty(host, key: keyPath.utf8Start, initial: initial, setter: setter)
40+
return associatedProperty(host, key: keyPath.utf8Start, initial: { host in
41+
host.valueForKeyPath(keyPath.stringValue) as? T ?? placeholder()
42+
}, setter: setter)
4743
}
4844

4945
/// Attaches a `MutableProperty` value to the `host` object under `key`. The property is
@@ -52,7 +48,7 @@ public func associatedProperty<T: AnyObject>(host: AnyObject, keyPath: StaticStr
5248
///
5349
/// This can be used as an alternative to `DynamicProperty` for creating strongly typed
5450
/// bindings on Cocoa objects.
55-
public func associatedProperty<Host: AnyObject, T>(host: Host, key: UnsafePointer<()>, initial: Host -> T, setter: (Host, T) -> ()) -> MutableProperty<T> {
51+
public func associatedProperty<Host: AnyObject, T>(host: Host, key: UnsafePointer<()>, @noescape initial: Host -> T, setter: (Host, T) -> ()) -> MutableProperty<T> {
5652
return associatedObject(host, key: key) { host in
5753
let property = MutableProperty(initial(host))
5854

@@ -69,7 +65,7 @@ public func associatedProperty<Host: AnyObject, T>(host: Host, key: UnsafePointe
6965
/// On first use attaches the object returned from `initial` to the `host` object using
7066
/// `key` via `objc_setAssociatedObject`. On subsequent usage, returns said object via
7167
/// `objc_getAssociatedObject`.
72-
public func associatedObject<Host: AnyObject, T: AnyObject>(host: Host, key: UnsafePointer<()>, initial: Host -> T) -> T {
68+
public func associatedObject<Host: AnyObject, T: AnyObject>(host: Host, key: UnsafePointer<()>, @noescape initial: Host -> T) -> T {
7369
var value = objc_getAssociatedObject(host, key) as? T
7470
if value == nil {
7571
value = initial(host)

0 commit comments

Comments
 (0)