您好,登錄后才能下訂單哦!
NSArray *aa = [NSArray arrayWithObjects:@"11", @"122", nil]; NSLog(@"%@", aa); //里面只有一對鍵值的字典 NSDictionary *dic1 = [NSDictionary dictionaryWithObject:@"2134" forKey:@"id"]; NSLog(@"%@", dic1); //多個鍵值的字典 NSArray *value =[NSArray arrayWithObjects:@"2134", @"迅雷", @"hudeifeifei", nil ]; //字典中, 是無序的 。。。 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); //字典長度 NSLog(@"%lu",[dic3 count]); //調出所有的key值 NSArray *allkey = [dic3 allKeys]; NSLog(@"%@", allkey); NSDictionary *dic4 = [NSDictionary dictionaryWithObjectsAndKeys:@"aa", @11, @"bb", @22, @"cc", @"33", nil];//value-key value-key @11--- 字面量,--語法糖 NSLog(@"%@", dic4); //可變字典bobo NSMutableDictionary *bobo = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"Cyuyan", @"bobo", nil]; NSLog(@"%@", bobo); //插入兩個鍵值 [bobo setObject:@"xing" forKey:@"xiaoliu"]; [bobo setObject:@"UI" forKey:@"laojiang"]; NSLog(@"%@", bobo); //插入一個鍵值 [bobo setValue:@"OC" forKeyPath:@"xiaoyunzi"]; NSLog(@"---------》%@",bobo); //對bobo對值踐行修改 [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]); // } //有問題。。。問題 // 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和其對應的值 [bobo removeObjectForKey:@"xiaoliu"]; NSLog(@"%@", bobo); //移除所有 // [bobo removeAllObjects]; // NSLog(@"%@", bobo); //移除數組定義的key和其對應的值 NSArray *re =[NSArray arrayWithObjects:@"bobo",@"xiaoyunzi", nil]; //里面都是key [bobo removeObjectsForKeys:re]; NSLog(@"%@",bobo);
字典簡介:
字典類用于保存具有映射關系(key-value對)的數據
一個key-value對認為是一個元素(實體),字典是存貯key-value對的容器
字典類的特點
于數組不同,數組靠下標存取數據,數據的下標是唯一的
字典靠key存取元素,key不能重復(如果重復只輸出第一個,而第二個沒有實際意義),value必須是對象
鍵值對在字典中是無序存儲的,,,,
NSDictionary
不可變字典
字典一旦創建,鍵值對就不可更改,不可添加,不可刪除
僅能讀取key或者value
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。