Skip to content

Instantly share code, notes, and snippets.

@SergLam
Last active September 3, 2020 15:57
Show Gist options
  • Save SergLam/88c3e46ee5388302d6630823eabfb81d to your computer and use it in GitHub Desktop.
Save SergLam/88c3e46ee5388302d6630823eabfb81d to your computer and use it in GitHub Desktop.
Weak set of object - inspired by article - https://nshipster.com/nshashtable-and-nsmaptable/
import Foundation
/// The WeakSet is weak: References to objects in the collection are held weakly.
/// If there is no other reference to an object stored in the WeakSet, they can be garbage collected.
public class WeakSet<T>: Sequence, ExpressibleByArrayLiteral, CustomStringConvertible, CustomDebugStringConvertible {
private var objects = NSHashTable<AnyObject>.weakObjects()
public init(_ objects: [T]) {
for object in objects {
insert(object)
}
}
public required convenience init(arrayLiteral elements: T...) {
self.init(elements)
}
public var allObjects: [T] {
return objects.allObjects as? [T] ?? []
}
public var count: Int {
return objects.count
}
public func contains(_ object: T) -> Bool {
return objects.contains(object as AnyObject)
}
public func insert(_ object: T) {
objects.add(object as AnyObject)
}
public func remove(_ object: T) {
objects.remove(object as AnyObject)
}
public func removeAll() {
objects.removeAllObjects()
}
public func makeIterator() -> AnyIterator<T> {
let iterator = objects.objectEnumerator()
return AnyIterator {
return iterator.nextObject() as? T
}
}
public var description: String {
return objects.description
}
public var debugDescription: String {
return objects.debugDescription
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment