日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS中对字典进行排序 ios开发教程

發布時間:2023/12/20 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS中对字典进行排序 ios开发教程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

iOS中字典實際上是無序的,那么如果我們要對字典進行排序要怎么做呢?

代碼如下:

@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
#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 先看一段代碼

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);}


這個結果可以看到和我們實際存入字典的順序是不一樣的

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);}

總結

以上是生活随笔為你收集整理的iOS中对字典进行排序 ios开发教程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。