OC秩序辞書

4220 ワード

ocには秩序辞書は存在せず、既存の秩序辞書はすべて無秩序辞書に基づいて、辞書で配列の2つの属性を加え、配列の秩序性を使用してキー値対データを組織する.コードを見てください
  • .h
  • @interface OrderedDictionary : NSMutableDictionary
    {
        NSMutableDictionary *dictionary;
        NSMutableArray *array;
    }
    
    //      
    - (void)insertObject:(id)anObject forKey:(id)aKey atIndex:(NSUInteger)anIndex;
    
    //       obj
    - (id)keyAtIndex:(NSUInteger)anIndex;
    
    //  
    - (NSEnumerator *)reverseKeyEnumerator;
    
    //  
    - (NSEnumerator *)keyEnumerator;
    
    //       
    -(void)filtrateChinese;
    
    
    @end
    
  • .m
  • #import "OrderedDictionary.h"
    
    NSString *DescriptionForObject(NSObject *object, id locale, NSUInteger indent)
    {
        NSString *objectString;
        if ([object isKindOfClass:[NSString class]])
        {
            objectString = [[NSString alloc]init];
        }
        else if ([object respondsToSelector:@selector(descriptionWithLocale:indent:)])
        {
            objectString = [(NSDictionary *)object descriptionWithLocale:locale indent:indent];
        }
        else if ([object respondsToSelector:@selector(descriptionWithLocale:)])
        {
            objectString = [(NSSet *)object descriptionWithLocale:locale];
        }
        else
        {
            objectString = [object description];
        }
        return objectString;
    }
    
    @implementation OrderedDictionary
    //     
    - (id)init
    {
        return [self initWithCapacity:0];
    }
    
    //     
    - (id)initWithCapacity:(NSUInteger)capacity
    {
        self = [super init];
        if (self != nil)
        {
            dictionary = [[NSMutableDictionary alloc] initWithCapacity:capacity];
            array = [[NSMutableArray alloc] initWithCapacity:capacity];
        }
        return self;
    }
    
    
    //copy  
    - (id)copy
    {
        return [self mutableCopy];
    }
    
    //    
    - (void)setObject:(id)anObject forKey:(id)aKey
    {
        if (![dictionary objectForKey:aKey])
        {
            [array addObject:aKey];
        }
        [dictionary setObject:anObject forKey:aKey];
    }
    
    - (void)removeObjectForKey:(id)aKey
    {
        [dictionary removeObjectForKey:aKey];
        [array removeObject:aKey];
    }
    
    - (NSUInteger)count
    {
        return [dictionary count];
    }
    
    - (id)objectForKey:(id)aKey
    {
        return [dictionary objectForKey:aKey];
    }
    
    - (NSEnumerator *)keyEnumerator
    {
        return [array objectEnumerator];
    }
    
    - (NSEnumerator *)reverseKeyEnumerator
    {
        return [array reverseObjectEnumerator];
    }
    
    - (void)insertObject:(id)anObject forKey:(id)aKey atIndex:(NSUInteger)anIndex
    {
        if ([dictionary objectForKey:aKey])
        {
            [self removeObjectForKey:aKey];
        }
        [array insertObject:aKey atIndex:anIndex];
        [dictionary setObject:anObject forKey:aKey];
    }
    
    - (id)keyAtIndex:(NSUInteger)anIndex
    {
        return [array objectAtIndex:anIndex];
    }
    
    //         ,           ,       。
    - (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level
    {
        NSMutableString *indentString = [NSMutableString string];
        NSUInteger i, count = level;
        for (i = 0; i < count; i++)
        {
            [indentString appendFormat:@"    "];
        }
        
        NSMutableString *description = [NSMutableString string];
        [description appendFormat:@"%@{
    ", indentString]; for (NSObject *key in self) { [description appendFormat:@"%@ %@ = %@;
    ", indentString, DescriptionForObject(key, locale, level), DescriptionForObject([self objectForKey:key], locale, level)]; } [description appendFormat:@"%@}
    ", indentString]; return description; } // -(void)filtrateChinese { for (id key in self.allKeys) { id object=[self objectForKey:key]; if ([object isKindOfClass:[NSString class]]) { [self setObject:(NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)object, NULL, NULL, kCFStringEncodingUTF8)) forKey:key]; } } } @end