OC中的字典

來源:互聯網
上載者:User

標籤:資料   string   類   set   cti   name   

// ********************不可變最字典*****************

       /* NSDictionary * dic = [NSDictionary dictionaryWithObject:@"張三" forKey:@"name" ];

        NSLog(@"%@", dic);//便利構造器建立字典

        

        //字典裡一個key只有一個vlaue, 但是一個value可以有好幾個key

        NSDictionary  * dic1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"zhangsan", @"name", @"boy", @"sex", @"toyke", @"address", nil];//初始化建立字典

        NSLog(@"%@", dic1);

        

        NSArray * value = [[NSArray alloc] initWithObjects:@"張三", @"李四", @"王五", @"趙六", nil];

        NSArray * key = [[NSArray alloc] initWithObjects:@"name3", @"name4", @"name5", @"name6", nil];

        NSDictionary * nk = [[NSDictionary alloc] initWithObjects:value forKeys:key];//初始化+數組建立字典

        NSLog(@"%@", nk);

        

        NSLog(@"%lu", [nk count]);//擷取索引值對的個數

        

        NSString * result = [nk objectForKey:@"name3"];//根據鍵來獲得相對應的值

        NSLog(@"%@", result);

        

        NSString * re = [nk objectForKey:@"name6"];

        NSString * re1 = [nk objectForKey:@"name4"];

        NSString * re3 = [nk objectForKey:@"name5"];

        NSLog(@"%@, %@, %@", re, re1, re3);

        

        NSArray * cou = [nk allKeys];//獲得所有的鍵

        for (int i = 0; i < [cou count]; i++) {

            NSString * key = [cou objectAtIndex:i];//獲得數組中的鍵

            NSString * value = [nk objectForKey:key];//用鍵來獲得值

            NSLog(@"%@", value);

        }

        

        

        NSArray * cou1 = [nk allKeys];

        for (int i = 0; i < [cou1 count]; i++) {

            NSLog(@"%@", [nk objectForKey:[cou objectAtIndex:i]]);

        }

        

        NSArray * values = [nk allValues];//獲得所有的值存放在數組中

        for (int i = 0; i < [values count]; i++) {

            id c = [values objectAtIndex:i];

            NSLog(@"%@", c);

        }*/

 

 

        // ********************可變最字典*****************

        NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:1];

        [dict setObject:@"zhangsan" forKey:@"name"];

        [dict setObject:@"boy" forKey:@"sex"];

        [dict setObject:@"18" forKey:@"age"];

        NSLog(@"%@", dict);

        [dict setObject:@"paoniu" forKey:@"hobby"];

        NSLog(@"%@", dict);

        NSDictionary  * dic1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"zhangsan1", @"name1", @"boy1", @"sex1", @"toyke1", @"address", nil];//初始化建立字典

        [dict setValuesForKeysWithDictionary:dic1];

        [dict removeObjectForKey:@"name1"];//移除索引值為name1的值

        

        NSNumber * num = [NSNumber numberWithInt:24];//用number把數字轉成對象

        

        [dict setObject:num forKey:@"age"];

        

        NSNumber * score = [NSNumber numberWithFloat:88.8];

        [dict setObject:score forKey:@"score"];

        

        [dict setObject:[NSNumber numberWithDouble:999.999] forKey:@"fight"];

      

        NSLog(@"%@", dict);

 

       float sco = [[dict objectForKey:@"score"] floatValue];//把對象轉成基礎資料型別 (Elementary Data Type)

        NSLog(@"%g", sco);

        

        

 

聯繫我們

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