Created
July 7, 2020 14:37
-
-
Save inso-/84cde85f83e8db4917b531ff121d891d to your computer and use it in GitHub Desktop.
Weak Property Wrapper + Hashable
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 | |
public struct Weak<Wrapped: AnyObject> { | |
private weak var value: AnyObject? | |
public init(_ value: Wrapped? = nil) { | |
self.value = value | |
} | |
public var wrappedValue: Wrapped? { | |
get { value as? Wrapped } | |
set { value = newValue } | |
} | |
} | |
extension Weak: Hashable { | |
public static func == (lhs: Weak<Wrapped>, rhs: Weak<Wrapped>) -> Bool { | |
lhs.hashValue == rhs.hashValue | |
} | |
public func hash(into hasher: inout Hasher) { | |
hasher.combine(unsafeBitCast(value, to: Int.self)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment