Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add noescape attribute for initial and placeholder closures of associatedProperty #70

Merged
merged 1 commit into from
Nov 26, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions Source/Foundation/Association.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,13 @@ public func associatedProperty(host: AnyObject, keyPath: StaticString) -> Mutabl
///
/// This can be used as an alternative to `DynamicProperty` for creating strongly typed
/// bindings on Cocoa objects.
///
/// N.B. Ensure that `host` isn't strongly captured by `placeholder`, otherwise this will
/// create a retain cycle with `host` causing it to never dealloc.
public func associatedProperty<T: AnyObject>(host: AnyObject, keyPath: StaticString, placeholder: () -> T) -> MutableProperty<T> {
let initial: AnyObject -> T = { host in
host.valueForKeyPath(keyPath.stringValue) as? T ?? placeholder()
}
public func associatedProperty<T: AnyObject>(host: AnyObject, keyPath: StaticString, @noescape placeholder: () -> T) -> MutableProperty<T> {
let setter: (AnyObject, T) -> () = { host, newValue in
host.setValue(newValue, forKeyPath: keyPath.stringValue)
}
return associatedProperty(host, key: keyPath.utf8Start, initial: initial, setter: setter)
return associatedProperty(host, key: keyPath.utf8Start, initial: { host in
host.valueForKeyPath(keyPath.stringValue) as? T ?? placeholder()
}, setter: setter)
}

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

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