Created
March 22, 2020 16:03
-
-
Save inso-/e51cf91ee3b144fff7183f58311b925a to your computer and use it in GitHub Desktop.
propertyWrapper WeakReference
This file contains 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 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