Skip to content

Instantly share code, notes, and snippets.

@akio0911
Forked from codelynx/ZMapTable.swift
Last active May 2, 2016 16:58
Show Gist options
  • Save akio0911/f3837d445891f182de71186a27e429ae to your computer and use it in GitHub Desktop.
Save akio0911/f3837d445891f182de71186a27e429ae 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
var creator: () -> NSMapTable {
switch self {
case .StrongToWeak: return NSMapTable.strongToWeakObjectsMapTable
case .WeakToStrong: return NSMapTable.weakToStrongObjectsMapTable
case .WeakToWeak: return NSMapTable.weakToWeakObjectsMapTable
case .StrongToStrong: return NSMapTable.strongToStrongObjectsMapTable
}
}
}
class ZMapTable<K: AnyObject, V: AnyObject>: CustomStringConvertible {
let mapTable: NSMapTable
init(option: ZMapTableOption) {
mapTable = option.creator()
}
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