Objective -C之字典

來源:互聯網
上載者:User

標籤:

 1         //字典:(關鍵字 值) 2 //        NSArray *array = [NSArray array];//空數組 3 //        NSDictionary *dictionary = [NSDictionary dictionary];//空字典 4         NSDictionary *my = [NSDictionary dictionaryWithObject:@"objective" forKey:@"key"]; 5                             NSLog(@"%@",my); 6         NSDictionary *to = [NSDictionary dictionaryWithObjectsAndKeys:@"123",@"abc",@"456",@"efg" ,nil];//建立包含多個值的字典 7         NSLog(@"%@",to); 8         NSDictionary *me = @{ 9                                @"a":@"1",10                                @"b":@"2"11                              };12         NSLog(@"%@,%li",me,me.count);13         NSString *s = [me objectForKey:@"a"];//索引值對應的對象14         NSString *ss = me[@"b"];//跟數組差不多15         NSLog(@"%@,%@",ss,s);16         17 //        NSArray *keyArr = [me allKeys];18 //        for(NSString *key in keyArr)19 //        {20 //            NSLog(@"%@ = %@",key,me[key]);21 //        }22       23         NSDictionary *niubi = [NSDictionary dictionaryWithObjectsAndKeys:@"liyuanfang",@"direnjie",@"Moran ",@"fuermosi",@"kenanfushou",@"kenan" ,nil];//建立包含多個值的字典24         NSArray *keyArr = [niubi allKeys];//取字典中的所有索引值25         for(NSString *key in keyArr)26         {27             NSLog(@"%@問%@這事你怎麼看?",key,niubi[key]);28         }29 /**********************************************************************************/30         //取出字典中所有的值31         NSArray *valueArr = [me allValues];32         NSLog(@"取字典中的所有鍵對應的值%@",valueArr);33         34         //字典既然存在,那麼開發人員肯定想到要方便的使用它35         //然而就有了,可以獨立取出鍵或值的方法:allKeys和allValues36         //因為他們是數組屬性,所以需要放到建立的數組對象中37         38         //列舉程式中OC語言中一個遍曆的有效方法,39         //應用時,一般先通過.....Enumerator擷取列舉程式,存放到其對應的類型NSEnumerator對象中40         //這個對象會有一個自動向下遍曆的方法nextObject41         //先擷取字典中key的列舉程式,然後遍曆該列舉程式,擷取key對應的值42         NSEnumerator *e = [me keyEnumerator];43         id obj;        44         while (obj = [e nextObject]) {45             NSLog(@"%@ = %@",obj,me[obj]);46         }47         48         //擷取value對應的列舉程式49         NSEnumerator *a = [me objectEnumerator];50         while(obj = [a nextObject])51         {52             NSLog(@"%@",obj);53         }54         55         //鍵和對象列舉塊,stop代表遍曆停止56         57         [me enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {58             59             NSLog(@"key=%@,value = %@",key,obj);60         }];

 

Objective -C之字典

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.