Skip to content

Instantly share code, notes, and snippets.

@inso-
Created March 22, 2020 16:03
Show Gist options
  • Save inso-/e51cf91ee3b144fff7183f58311b925a to your computer and use it in GitHub Desktop.
Save inso-/e51cf91ee3b144fff7183f58311b925a to your computer and use it in GitHub Desktop.
propertyWrapper WeakReference
@propertyWrapper
struct WeakReference<T> {
private weak var storage: AnyObject? = nil
private var value: T?{
get { return storage.map { $0 as! T } }
set {
storage = newValue.map {
let asObject = $0 as AnyObject
assert(asObject === $0 as AnyObject)
return asObject
}
}
}
init(value: T? = nil) {
self.value = value
}
var wrappedValue: T? {
get { value }
set { value = newValue }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment