Skip to content

Instantly share code, notes, and snippets.

@codelynx
Last active May 2, 2016 16:58
Show Gist options
  • Save codelynx/cadf72f1bbba5f453dd9 to your computer and use it in GitHub Desktop.
Save codelynx/cadf72f1bbba5f453dd9 to your computer and use it in GitHub Desktop.
// ZMapTable.swift
//
// Copyright (c) 2016 Kaz Yoshikawa. Released under MIT License.
enum ZMapTableOption {
case StrongToWeak
case WeakToStrong
case WeakToWeak
case StrongToStrong
}
class ZMapTable<K: AnyObject, V: AnyObject>: CustomStringConvertible {
let mapTable: NSMapTable
init(option: ZMapTableOption) {
switch option {
case .StrongToWeak: self.mapTable = NSMapTable.strongToWeakObjectsMapTable()
case .WeakToStrong: self.mapTable = NSMapTable.weakToStrongObjectsMapTable()
case .WeakToWeak: self.mapTable = NSMapTable.weakToWeakObjectsMapTable()
case .StrongToStrong: self.mapTable = NSMapTable.strongToStrongObjectsMapTable()
}
}
subscript(key: K) -> V? {
get { return self.mapTable.objectForKey(key) as? V }
set { self.mapTable.setObject(newValue, forKey: key) }
}
var description: String {
return self.mapTable.description
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment