Created
March 26, 2016 07:12
-
-
Save codelynx/791b34482c15df7868c9 to your computer and use it in GitHub Desktop.
NSMapTable wrapper
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 | |
// ZKit | |
// | |
// Created by Kaz Yoshikawa on 3/26/16. | |
// Copyright © 2016 Electricwoods LLC. All rights reserved. | |
// | |
import Foundation | |
class ZStronToWeakMapTable<K: AnyObject, V: AnyObject>: CustomStringConvertible { | |
let mapTable: NSMapTable | |
init() { | |
self.mapTable = NSMapTable.strongToWeakObjectsMapTable() | |
} | |
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 ZWeakToStrongMapTable<K: AnyObject, V: AnyObject>: CustomStringConvertible { | |
let mapTable: NSMapTable | |
init() { | |
self.mapTable = NSMapTable.weakToStrongObjectsMapTable() | |
} | |
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 | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment