Created
July 28, 2020 03:55
-
-
Save dduan/8e5fe939b53fc49177cc15cd36f591f5 to your computer and use it in GitHub Desktop.
This property wrapper guarantees that a closure does nothing if its enclosing object is deinitialized.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@propertyWrapper | |
struct Escaping { | |
typealias Closure = () -> Void | |
var store: Closure | |
var wrappedValue: Closure { | |
get { self.store } | |
set { self.store = newValue } | |
} | |
init(wrappedValue: @escaping Closure) { | |
self.store = wrappedValue | |
} | |
static subscript<EnclosingSelf: AnyObject, FinalValue>( | |
_enclosingInstance observed: EnclosingSelf, | |
wrapped wrappedKeyPath: ReferenceWritableKeyPath<EnclosingSelf, FinalValue>, | |
storage storageKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Self> | |
) -> Closure | |
{ | |
get { | |
{ [weak observed] in | |
guard let this = observed else { | |
return | |
} | |
this[keyPath: storageKeyPath].store() | |
} | |
} | |
set { | |
observed[keyPath: storageKeyPath].store = newValue | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Thing { | |
@Escaping | |
var method = { | |
print(42) | |
} | |
} | |
var f: () -> Void = {} | |
do { | |
let t = Thing() | |
f = t.method | |
f() // prints 42 | |
} | |
f() // does not print 42 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment