File tree Expand file tree Collapse file tree
Example/iOS_Example/iOS_Example
Sources/ReerKit/Utility/Weak Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ class KeyboardManagerDemoViewController: UIViewController {
1313
1414 deinit {
1515 print ( #function)
16+ KeyboardManager . shared. removeObserver ( self )
1617 }
1718
1819 var defaultFrame : CGRect = . zero
Original file line number Diff line number Diff line change 2323public final class Weak < T: AnyObject > {
2424 public private( set) weak var object : T ?
2525 private let hashKey : Int
26+
2627 public init ( _ object: T ) {
2728 self . object = object
2829 self . hashKey = ObjectIdentifier ( object) . hashValue
2930 }
31+
32+ /// Creates a lookup-only instance that can be used for set operations
33+ /// without storing a weak reference. Useful when the object may be deallocating.
34+ public init ( hashKeyOnly object: T ) {
35+ self . object = nil
36+ self . hashKey = ObjectIdentifier ( object) . hashValue
37+ }
3038}
3139
3240extension Weak : Equatable , Hashable {
Original file line number Diff line number Diff line change @@ -53,7 +53,7 @@ public class WeakSet<T: AnyObject>: ExpressibleByArrayLiteral {
5353 }
5454
5555 public func contains( _ object: T ) -> Bool {
56- return objectSet. contains ( Weak ( object) )
56+ return objectSet. contains ( Weak ( hashKeyOnly : object) )
5757 }
5858
5959 public func add( _ object: T ) {
@@ -74,11 +74,11 @@ public class WeakSet<T: AnyObject>: ExpressibleByArrayLiteral {
7474 }
7575
7676 public func remove( _ object: T ) {
77- objectSet. remove ( Weak ( object) )
77+ objectSet. remove ( Weak ( hashKeyOnly : object) )
7878 }
7979
8080 public func removeObjects( _ objects: [ T ] ) {
81- objectSet. subtract ( objects. map { Weak ( $0) } )
81+ objectSet. subtract ( objects. map { Weak ( hashKeyOnly : $0) } )
8282 }
8383
8484 public func removeAll( ) {
You can’t perform that action at this time.
0 commit comments