Skip to content

Instantly share code, notes, and snippets.

@liamnichols
Created December 9, 2013 09:12
Show Gist options
  • Save liamnichols/7869468 to your computer and use it in GitHub Desktop.
Save liamnichols/7869468 to your computer and use it in GitHub Desktop.
//
// Created by Liam Nichols on 14/10/2013.
// Copyright (c) 2013 Mubaloo Ltd. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface LNOrderedMutableDictionary : NSMutableDictionary
///If `anObject` is nil, it will not be added to the dictionary.
- (void)setNothingIfNil:(id)anObject forKey:(id)aKey;
@end
//
// Created by Liam Nichols on 14/10/2013.
// Copyright (c) 2013 Mubaloo Ltd. All rights reserved.
//
#import "LNOrderedMutableDictionary.h"
@interface LNOrderedMutableDictionary ()
@property (nonatomic, strong) NSMutableDictionary *dictionary;
@property (nonatomic, strong) NSMutableOrderedSet *array;
@end
@implementation LNOrderedMutableDictionary
- (id)initWithCapacity:(NSUInteger)capacity
{
self = [super init];
if (self != nil)
{
self.dictionary = [[NSMutableDictionary alloc] initWithCapacity:capacity];
self.array = [[NSMutableOrderedSet alloc] initWithCapacity:capacity];
}
return self;
}
- (id)init
{
self = [self initWithCapacity:0];
if (self)
{
}
return self;
}
- (void)setObject:(id)anObject forKey:(id)aKey
{
[self.array removeObject:aKey];
[self.array addObject:aKey];
[self.dictionary setObject:anObject forKey:aKey];
}
- (void)setNothingIfNil:(id)anObject forKey:(id)aKey
{
if (anObject != nil)
[self setObject:anObject forKey:aKey];
}
- (void)removeObjectForKey:(id)aKey
{
[self.dictionary removeObjectForKey:aKey];
[self.array removeObject:aKey];
}
- (NSUInteger)count
{
return [self.dictionary count];
}
- (id)objectForKey:(id)aKey
{
return [self.dictionary objectForKey:aKey];
}
- (NSEnumerator *)keyEnumerator
{
return [self.array objectEnumerator];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment