溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

NSDictionary常用用法

發(fā)布時(shí)間:2020-07-10 17:58:10 來(lái)源:網(wǎng)絡(luò) 閱讀:568 作者:Im劉亞芳 欄目:開(kāi)發(fā)技術(shù)
NSArray *aa = [NSArray arrayWithObjects:@"11", @"122", nil];
        NSLog(@"%@", aa);
        //里面只有一對(duì)鍵值的字典
        NSDictionary *dic1 = [NSDictionary dictionaryWithObject:@"2134" forKey:@"id"];
        NSLog(@"%@", dic1);
        
        //多個(gè)鍵值的字典
        NSArray *value =[NSArray arrayWithObjects:@"2134", @"迅雷", @"hudeifeifei", nil ];  //字典中,  是無(wú)序的 。。。
        NSArray *key = [NSArray arrayWithObjects:@"id", @"topic", @"tfr",nil ];
        NSDictionary *dic2 = [NSDictionary dictionaryWithObjects:value forKeys:key];
        NSLog(@"%@", dic2);
        
        NSArray *value1 = [NSArray arrayWithObjects:@"xiyouji",@"hongloumeng",@"shuihuzhuan",@"sanuoyanyi", nil];
        NSArray *key1 = [NSArray arrayWithObjects:@"guoguanzhong",@"wuchengen",@"ximenqi",@"caocao", nil];
        NSDictionary *dic3 = [NSDictionary dictionaryWithObjects:value1 forKeys:key1];
        NSLog(@"%@", dic3);
        [dic3 objectForKey:@"xiyouji"];    //
        NSLog(@"------%@", dic3);
        //字典長(zhǎng)度
        NSLog(@"%lu",[dic3 count]);
        //調(diào)出所有的key值
        NSArray *allkey = [dic3 allKeys];
        NSLog(@"%@", allkey);
        
        NSDictionary *dic4 = [NSDictionary dictionaryWithObjectsAndKeys:@"aa", @11, @"bb", @22, @"cc", @"33", nil];//value-key   value-key @11---  字面量,--語(yǔ)法糖
        NSLog(@"%@", dic4);
        //可變字典bobo
        NSMutableDictionary *bobo = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"Cyuyan", @"bobo", nil];
        NSLog(@"%@", bobo);
        //插入兩個(gè)鍵值
        [bobo setObject:@"xing" forKey:@"xiaoliu"];
        [bobo setObject:@"UI" forKey:@"laojiang"];
        NSLog(@"%@", bobo);
        //插入一個(gè)鍵值
        [bobo setValue:@"OC" forKeyPath:@"xiaoyunzi"];
        NSLog(@"---------》%@",bobo);
        //對(duì)bobo對(duì)值踐行修改
        [bobo setObject:@"sisheng" forKeyedSubscript:@"bobo"];
        NSLog(@"1111111%@",bobo);
        //使用forin遍歷字典所有的key
        for (NSString *i in bobo) {
            NSLog(@"%@",[bobo objectForKey:i]);
        }
//        NSInteger count1 = [bobo count];
//        for (int i = 1; i < count1; i++) {
//            NSLog(@"......%@", [bobo objectForKey:i]);
//        }
        
        
        //有問(wèn)題。。。問(wèn)題
//        NSArray *allKeys = [bobo allKeys];
//        for (int i = 0; i < [bobo count]; i++) {
//            NSString *key = [allKeys objectsAtIndexes:i];
//            NSString *valu = [bobo objectForKey:key];
//            NSLog(@"%@",key, valu);
//        }
        //移除xiaoliu和其對(duì)應(yīng)的值
        [bobo removeObjectForKey:@"xiaoliu"];
        NSLog(@"%@", bobo);
        //移除所有
//        [bobo removeAllObjects];
//        NSLog(@"%@", bobo);
        //移除數(shù)組定義的key和其對(duì)應(yīng)的值
        NSArray *re =[NSArray arrayWithObjects:@"bobo",@"xiaoyunzi", nil];  //里面都是key
        [bobo removeObjectsForKeys:re];
        NSLog(@"%@",bobo);

字典簡(jiǎn)介:

  • 字典類(lèi)用于保存具有映射關(guān)系(key-value對(duì))的數(shù)據(jù)

  • 一個(gè)key-value對(duì)認(rèn)為是一個(gè)元素(實(shí)體),字典是存貯key-value對(duì)的容器

字典類(lèi)的特點(diǎn)

  1. 于數(shù)組不同,數(shù)組靠下標(biāo)存取數(shù)據(jù),數(shù)據(jù)的下標(biāo)是唯一的

  2. 字典靠key存取元素,key不能重復(fù)(如果重復(fù)只輸出第一個(gè),而第二個(gè)沒(méi)有實(shí)際意義),value必須是對(duì)象

  3. 鍵值對(duì)在字典中是無(wú)序存儲(chǔ)的,,,,

NSDictionary

  1. 不可變字典

  2. 字典一旦創(chuàng)建,鍵值對(duì)就不可更改,不可添加,不可刪除

  3. 僅能讀取key或者value



向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI