[Objective-C] 009_Foundation架構之NSDictionary與NSMutableDictionary

來源:互聯網
上載者:User

標籤:

  在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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.