Skip to content

Commit

Permalink
Added Swift
Browse files Browse the repository at this point in the history
  • Loading branch information
mickmaccallum committed Sep 11, 2015
1 parent 9a7baad commit cb21e80
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions 2013-08-19-nshashtable-and-nsmaptable.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ category: Cocoa
tags: nshipster
excerpt: "NSSet and NSDictionary, along with NSArray are the workhorse collection classes of Foundation. Unlike other standard libraries, implementation details are hidden from developers, allowing them to write simple code and trust that it will be (reasonably) performant."
status:
swift: t.b.c.
swift: 2.0
reviewed: September 11, 2015
---

`NSSet` and `NSDictionary`, along with `NSArray` are the workhorse collection classes of Foundation. Unlike [ other standard libraries](http://en.wikipedia.org/wiki/Java_collections_framework), implementation details are [hidden](http://ridiculousfish.com/blog/posts/array.html) from developers, allowing them to write simple code and trust that it will be (reasonably) performant.
Expand All @@ -28,6 +29,15 @@ So without further ado, here's everything you need to know about two of the more

### Usage

~~~{swift}
let hashTable = NSHashTable(options: .CopyIn)
hashTable.addObject("foo")
hashTable.addObject("bar")
hashTable.addObject(42)
hashTable.removeObject("bar")
print(hashTable.allObjects)
~~~

~~~{objective-c}
NSHashTable *hashTable = [NSHashTable hashTableWithOptions:NSPointerFunctionsCopyIn];
[hashTable addObject:@"foo"];
Expand Down Expand Up @@ -59,6 +69,13 @@ NSLog(@"Members: %@", [hashTable allObjects]);

Instances where one might use `NSMapTable` include non-copyable keys and storing weak references to keyed delegates or another kind of weak object.

~~~{swift}
let delegate: AnyObject = ...
let mapTable = NSMapTable(keyOptions: .StrongMemory, valueOptions: .WeakMemory)
mapTable.setObject(delegate, forKey: "foo")
print("Keys: \(mapTable.keyEnumerator().allObjects)")
~~~

~~~{objective-c}
id delegate = ...;
NSMapTable *mapTable = [NSMapTable mapTableWithKeyOptions:NSMapTableStrongMemory
Expand All @@ -80,17 +97,31 @@ Equal to `NSPointerFunctionsObjectPointerPersonality`.

`NSMapTable` doesn't implement [object subscripting](http://nshipster.com/object-subscripting/), but it can be trivially added in a category. `NSDictionary`'s `NSCopying` requirement for keys belongs to `NSDictionary` alone:

~~~{swift}
extension NSMapTable {
subscript(key: AnyObject?) -> AnyObject? {
get {
return objectForKey(key)
}
set {
setObject(newValue, forKey: key)
}
}
}
~~~

~~~{objective-c}
@implementation NSMapTable (NSHipsterSubscripting)
- (id)objectForKeyedSubscript:(id)key
{
return [self objectForKey:key];
return [self objectForKey:key];
}
- (void)setObject:(id)obj forKeyedSubscript:(id)key
{
[self setObject:obj forKey:key];
[self setObject:obj forKey:key];
}
@end
Expand Down

0 comments on commit cb21e80

Please sign in to comment.