Skip to content

Instantly share code, notes, and snippets.

@setoh2000
Created April 10, 2013 18:03
Show Gist options
  • Save setoh2000/5356966 to your computer and use it in GitHub Desktop.
Save setoh2000/5356966 to your computer and use it in GitHub Desktop.
順序を保存するNSMutableDictionary NSMutableDictionaryなどを継承するのが結構大変なのでNSMutableDictionaryとキーを保持するNSMutableArrayを内包したものを自作。 自分が使うメンバーしか実装していない。
#import <Foundation/Foundation.h>
@interface MSOrderdDictionary : NSObject <NSCoding>
{
NSMutableDictionary *dictionary;
NSMutableArray *array;
}
+ (id)dictionary;
+ (id)dictionaryWithCapacity:(NSUInteger)numItems;
- (id)initWithDictionary:(MSOrderdDictionary *)srcDictionary copyItems:(BOOL)flag;
- (NSUInteger)count;
- (id)objectForKey:(id)aKey;
- (NSEnumerator *)keyEnumerator;
- (void)setObject:(id)anObject forKey:(id)aKey;
- (void)removeObjectForKey:(id)aKey;
- (void)removeAllObjects;
- (NSArray *)allValues;
@end
#import "MSOrderdDictionary.h"
@implementation MSOrderdDictionary
+ (id)dictionary
{
return [MSOrderdDictionary dictionaryWithCapacity:10];
}
+ (id)dictionaryWithCapacity:(NSUInteger)numItems
{
return [[MSOrderdDictionary alloc] initWithCapacity:numItems];
}
- (id)initWithDictionary:(MSOrderdDictionary *)srcDictionary copyItems:(BOOL)flag
{
self = [super init];
if (flag) {
self->dictionary = [NSMutableDictionary dictionaryWithDictionary:srcDictionary->dictionary];
self->array = [NSMutableArray arrayWithArray:srcDictionary->array];
}
else {
self->dictionary = srcDictionary->dictionary;
self->array = srcDictionary->array;
}
return self;
}
- (id)initWithCapacity:(NSUInteger)numItems
{
self = [super init];
self->dictionary = [[NSMutableDictionary alloc] initWithCapacity:numItems];
self->array = [[NSMutableArray alloc] initWithCapacity:numItems];
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject:dictionary forKey:@"MSOrderdDictionary_dictionary"];
[encoder encodeObject:array forKey:@"MSOrderdDictionary_array"];
}
- (id)initWithCoder:(NSCoder *)decoder
{
self = [super init];
self->dictionary = [decoder decodeObjectForKey:@"MSOrderdDictionary_dictionary"];
self->array = [decoder decodeObjectForKey:@"MSOrderdDictionary_array"];
return self;
}
- (NSUInteger)count
{
return [dictionary count];
}
- (id)objectForKey:(id)aKey
{
return [dictionary objectForKey:aKey];
}
- (NSEnumerator *)keyEnumerator
{
return [dictionary keyEnumerator];
}
- (NSEnumerator *)enumerator
{
return [dictionary keyEnumerator];
}
- (void)setObject:(id)anObject forKey:(id)aKey
{
[dictionary setObject:anObject forKey:aKey];
if (![array containsObject:aKey]) {
[array addObject:aKey];
}
}
- (void)removeObjectForKey:(id)aKey
{
[array removeObject:aKey];
[dictionary removeObjectForKey:aKey];
}
- (void)removeAllObjects
{
[dictionary removeAllObjects];
[array removeAllObjects];
}
- (NSArray *)allValues
{
return [dictionary objectsForKeys:array notFoundMarker:[NSNull null]];
}
@end
@KatagiriSo
Copy link

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment