Latest 2.0.2
Homepage https://github.com/nicholascross/WeakDictionary
License MIT
Platforms osx 10.9, ios 9.0, tvos 9.0, watchos 2.0, requires ARC
Authors

build status
code coverage
carthage compatible
CocoaPods compatible
GitHub release
Swift 4.2.x
platforms

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.

What are some differences from NSMapTable?

  • Use of equality operator for key comparison
  • Supports suscripts and Collection protocol 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

WeakDictionary

  • Values stored in the WeakDictionary are not retained
  • Keys must implement the Hashable protocol
  • weakDictionary will create a new WeakDictionary with any orphaned value references removed
  • weakKeyDictionary will create a new WeakKeyDictionary with any orphaned value references removed, this will only work if the key is a class type
  • dictionary will create a new swift dictionary excluding any nullified value references
  • reap will 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 { }

WeakKeyDictionary

  • Keys & values stored in the WeakKeyDictionary are not retained
  • Keys must implement the Hashable protocol
  • weakDictionary will create a new WeakDictionary with any orphaned value references removed
  • weakKeyDictionary will create a new WeakKeyDictionary with any orphaned value references removed
  • dictionary will create a new swift dictionary excluding any nullified key or value references
  • reap will 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
        }
    }

Latest podspec

{
    "name": "WeakDictionary",
    "version": "2.0.2",
    "summary": "A naive (strong key/weak value) dictionary & (weak key/weak value) dictionary implementations in swift",
    "homepage": "https://github.com/nicholascross/WeakDictionary",
    "license": "MIT",
    "authors": {
        "Nicholas Cross": "[email protected]"
    },
    "platforms": {
        "osx": "10.9",
        "ios": "9.0",
        "tvos": "9.0",
        "watchos": "2.0"
    },
    "source": {
        "git": "https://github.com/nicholascross/WeakDictionary.git",
        "tag": "2.0.2"
    },
    "source_files": "WeakDictionary/*.swift",
    "requires_arc": true,
    "swift_version": "4.2"
}

Pin It on Pinterest

Share This