ios字典选取城市的地址是怎么排序的

 我来答
匿名用户
2016-09-01
展开全部
代码如下:

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
<span style="font-size:18px;">  
 
@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;  
@end  
</span>  

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
<span style="font-size:18px;">  
 
#import "OrderedDictionary.h"  
 
 
NSString *DescriptionForObject(NSObject *object, id locale, NSUInteger indent)  
{  
    NSString *objectString;  
    if ([object isKindOfClass:[NSString class]])  
    {  
        objectString = (NSString *)[[object retain] autorelease];  
    }  
    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;  
}  
 
//析构方法  
- (void)dealloc  
{  
    [dictionary release];  
    [array release];  
    [super dealloc];  
}  
 
//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:@"%@{\n", indentString];  
    for (NSObject *key in self)  
    {  
        [description appendFormat:@"%@    %@ = %@;\n",  
            indentString,  
            DescriptionForObject(key, locale, level),  
            DescriptionForObject([self objectForKey:key], locale, level)];  
    }  
    [description appendFormat:@"%@}\n", indentString];  
    return description;  
}  
 
@end  
</span>  

先看一段代码
[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
<span style="font-size:18px;">  
 
NSMutableDictionary *dict2 = [[NSMutableDictionary alloc]initWithCapacity:0];  
    [dict2 setObject:@"one" forKey:@"1"];  
    [dict2 setObject:@"two" forKey:@"2"];  
    [dict2 setObject:@"four" forKey:@"4"];  
    [dict2 setObject:@"three" forKey:@"3"];  
      
      
      
    for (NSString *str in [dict2 allKeys]) {  
        NSLog(@"key == %@",str);  
    }  
</span>  

这个结果可以看到和我们实际存入字典的顺序是不一样的

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
<span style="font-size:18px;">  
 
OrderedDictionary *dict = [[OrderedDictionary alloc]initWithCapacity:0];  
      
    [dict setObject:@"two" forKey:@"2"];  
    [dict setObject:@"four" forKey:@"4"];  
    [dict setObject:@"three" forKey:@"3"];  
    [dict setObject:@"one" forKey:@"1"];  
      
    [dict insertObject:@"five" forKey:@"5" atIndex:0];  
 
    NSEnumerator *enumerator2 = [dict keyEnumerator];  
 
    id obj;  
    while(obj = [enumerator2 nextObject])  
    {  
        NSLog(@"%@",obj);  
    }</span>
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式