Last active
September 26, 2018 01:20
-
-
Save codelynx/5e24a436ae3c4d91df33 to your computer and use it in GitHub Desktop.
Swifty NSMapTable that can deal with weak referenced object - subclassing edition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ZMapTable.swift | |
// | |
// Copyright (c) 2016 Kaz Yoshikawa. Released under MIT License. | |
class ZMapTable<K: AnyObject, V: AnyObject>: CustomStringConvertible { | |
let mapTable: NSMapTable | |
private init(mapTable: NSMapTable) { | |
self.mapTable = mapTable | |
} | |
subscript(key: K) -> V? { | |
get { return self.mapTable.objectForKey(key) as? V } | |
set { self.mapTable.setObject(newValue, forKey: key) } | |
} | |
var description: String { | |
return mapTable.description | |
} | |
} | |
class ZStrongToWeakMapTable<K: AnyObject, V: AnyObject>: ZMapTable<K, V> { | |
init() { | |
super.init(mapTable: NSMapTable.strongToWeakObjectsMapTable()) | |
} | |
} | |
class ZWeakToStrongMapTable<K: AnyObject, V: AnyObject>: ZMapTable<K, V> { | |
init() { | |
super.init(mapTable: NSMapTable.weakToStrongObjectsMapTable()) | |
} | |
} | |
class ZWeakToWeakMapTable<K: AnyObject, V: AnyObject>: ZMapTable<K, V> { | |
init() { | |
super.init(mapTable: NSMapTable.weakToWeakObjectsMapTable()) | |
} | |
} | |
class ZStrongToStrongMapTable<K: AnyObject, V: AnyObject>: ZMapTable<K, V> { | |
init() { | |
super.init(mapTable: NSMapTable.strongToStrongObjectsMapTable()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample (note: String is not AnyObject use NSString)