A naive (strong key/weak value) dictionary & (weak key/weak value) dictionary implementation in swift.
Apple provides an existing implementation and you should use it instead. NSMapTable also has the advantage that you dont need to manually trigger clean up of old nil references.
It is also worth considering if clean up of references is even needed for your scenario. In cases where the dictionary itself is shortlived or the values are likely to be recreated for the same keys then it is probably sufficient to use a standard swift dictionary with a weak box wrapper instead.
What are some differences from NSMapTable?
- Use of equality operator for key comparison
- Supports suscripts and
Collectionprotocol inherited behaviour - Keys can optionally retain values as long as the key itself is retained the associated value can be retained
- Manual nil reference clean up required. Weak references are reclaimed as normal but container objects are left holding nil references until reaping is triggered
- Values stored in the
WeakDictionaryare not retained - Keys must implement the
Hashableprotocol weakDictionarywill create a newWeakDictionarywith any orphaned value references removedweakKeyDictionarywill create a newWeakKeyDictionarywith any orphaned value references removed, this will only work if the key is a class typedictionarywill create a new swift dictionary excluding any nullified value referencesreapwill remove any orphaned value references for mutable dictionaries
var dictionary = WeakDictionary<String, ExampleValue>()
var value: ExampleValue? = ExampleValue()
dictionary["key"] = value
print("\(dictionary["key"] != nil ? "has value" : "value missing")")
//prints: has value
value = nil
print("\(dictionary["key"] != nil ? "has value" : "value missing")")
//prints: value missing
private class ExampleValue { }- Keys & values stored in the
WeakKeyDictionaryare not retained - Keys must implement the
Hashableprotocol weakDictionarywill create a newWeakDictionarywith any orphaned value references removedweakKeyDictionarywill create a newWeakKeyDictionarywith any orphaned value references removeddictionarywill create a new swift dictionary excluding any nullified key or value referencesreapwill remove any orphaned key or value references for mutable dictionaries- Optionally values may be retained by the key using
WeakKeyDictionary(valuesRetainedByKey: true), the values will be released only after key references are reaped
var dictionary = WeakKeyDictionary<ExampleKey, ExampleValue>()
var transientKey: ExampleKey = ExampleKey(name: "value")
let retainedValue: ExampleValue? = ExampleValue()
dictionary[transientKey] = retainedValue
print("\(dictionary[transientKey] != nil ? "an example exits" : "no example exits")")
//prints: an example exits
transientKey = ExampleKey(name: "anothervalue")
let oldKey = ExampleKey(name: "value")
print("\(dictionary[oldKey] != nil ? "an example exits" : "no example exits")")
//prints: no example exits
print("number of item in dictionary \(dictionary.count)")
//prints: number of item in dictionary 1
//This is because nil key/value references are not automatically nullified when the key or value is deallocated
print("number of item in reaped dictionary \(dictionary.weakKeyDictionary().count)")
//prints: number of item in reaped dictionary 0
//Reaping the dictionary removes any keys without values and values not referenced by any key
private class ExampleValue { }
private class ExampleKey: Hashable {
let value: String
init(name: String) {
value = name
}
public static func == (lhs: ExampleKey, rhs: ExampleKey) -> Bool {
return lhs.value == rhs.value
}
public var hashValue: Int {
return value.hash
}
}