標籤:
在Cocoa Foundation中NSDictionary和NSMutableDictionary 用於對象有序集合,NSDictionary和NSMutableDictionary 與 NSArray和NSMutableArray 有些不一樣,前者可以儲存多個不同類型的資料,而後者只能儲存同一類型的資料。
1.NSDictionary 建立
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"SuperDo",@"teamName",@"SuperDo.Horse",@"teamMember", nil];
2.NSDictionary 遍曆
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"SuperDo",@"teamName",@"SuperDo.Horse",@"teamMember", nil];//得到詞典的數量int objectCount = (int)[dictionary count];NSLog(@"詞典的KEY數量為: %d",objectCount);//擷取詞典中所有KEY值NSEnumerator * enumeratorKey = [dictionary keyEnumerator];//快速枚舉遍曆所有KEY的值for (NSObject *object in enumeratorKey) { NSLog(@"KEY的值---》: %@",object);}//擷取詞典中所有Value值NSEnumerator * enumeratorValue = [dictionary objectEnumerator];//快速枚舉遍曆所有Value的值for (NSObject *object in enumeratorValue) { NSLog(@"Value的值---》: %@",object);}
3.NSDictionary 通過KEY找到的value
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"SuperDo",@"teamName",@"SuperDo.Horse",@"teamMember", nil];NSObject *object = [dictionary objectForKey:@"teamName"];if (object != nil) { NSLog(@"通過KEY找到的value是: %@",object);}
4.NSMutableDictionary 建立
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:10];
5.NSMutableDictionary 元素操作
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:10]; //向詞典中動態添加資料[dictionary setObject:@"SuperDo" forKey:@"teamName"];[dictionary setObject:@"SuperDo.Horse" forKey:@"teamMember"];//通過KEY找到valueNSObject *object = [dictionary objectForKey:@"teamName"];//擷取所有的keyNSArray *allKeys = [dictionary allKeys];//擷取所有的valueNSArray *allValues = [dictionary allValues];
本站文章為 寶寶巴士 SD.Team 原創,轉載務必在明顯處註明:(作者官方網站: 寶寶巴士 )
轉載自【寶寶巴士SuperDo團隊】 原文連結: http://www.cnblogs.com/superdo/p/4594337.html
[Objective-C] 009_Foundation架構之NSDictionary與NSMutableDictionary