NSArray*aa=[NSArrayarrayWithObjects:@"11",@"122",nil];NSLog(@"%@",aa);//里面只有一对键值的字典NSDictionary*dic1=[NSDictionarydictionaryWithObject:@"2134"forKey:@"id"];NSLog(@"%@",dic1);//多个键值的字典NSArray*value=[NSArrayarrayWithObjects:@"2134",@"迅雷",@"hudeifeifei",nil];//字典中,是无序的。。。NSArray*key=[NSArrayarrayWithObjects:@"id",@"topic",@"tfr",nil];NSDictionary*dic2=[NSDictionarydictionaryWithObjects:valueforKeys:key];NSLog(@"%@",dic2);NSArray*value1=[NSArrayarrayWithObjects:@"xiyouji",@"hongloumeng",@"shuihuzhuan",@"sanuoyanyi",nil];NSArray*key1=[NSArrayarrayWithObjects:@"guoguanzhong",@"wuchengen",@"ximenqi",@"caocao",nil];NSDictionary*dic3=[NSDictionarydictionaryWithObjects:value1forKeys:key1];NSLog(@"%@",dic3);[dic3objectForKey:@"xiyouji"];//NSLog(@"------%@",dic3);//字典长度NSLog(@"%lu",[dic3count]);//调出所有的key值NSArray*allkey=[dic3allKeys];NSLog(@"%@",allkey);NSDictionary*dic4=[NSDictionarydictionaryWithObjectsAndKeys:@"aa",@11,@"bb",@22,@"cc",@"33",nil];//value-keyvalue-key@11---字面量,--语法糖NSLog(@"%@",dic4);//可变字典boboNSMutableDictionary*bobo=[NSMutableDictionarydictionaryWithObjectsAndKeys:@"Cyuyan",@"bobo",nil];NSLog(@"%@",bobo);//插入两个键值[bobosetObject:@"xing"forKey:@"xiaoliu"];[bobosetObject:@"UI"forKey:@"laojiang"];NSLog(@"%@",bobo);//插入一个键值[bobosetValue:@"OC"forKeyPath:@"xiaoyunzi"];NSLog(@"---------》%@",bobo);//对bobo对值践行修改[bobosetObject:@"sisheng"forKeyedSubscript:@"bobo"];NSLog(@"1111111%@",bobo);//使用forin遍历字典所有的keyfor(NSString*iinbobo){NSLog(@"%@",[boboobjectForKey:i]);}//NSIntegercount1=[bobocount];//for(inti=1;i<count1;i++){//NSLog(@"......%@",[boboobjectForKey:i]);//}//有问题。。。问题//NSArray*allKeys=[boboallKeys];//for(inti=0;i<[bobocount];i++){//NSString*key=[allKeysobjectsAtIndexes:i];//NSString*valu=[boboobjectForKey:key];//NSLog(@"%@",key,valu);//}//移除xiaoliu和其对应的值[boboremoveObjectForKey:@"xiaoliu"];NSLog(@"%@",bobo);//移除所有//[boboremoveAllObjects];//NSLog(@"%@",bobo);//移除数组定义的key和其对应的值NSArray*re=[NSArrayarrayWithObjects:@"bobo",@"xiaoyunzi",nil];//里面都是key[boboremoveObjectsForKeys:re];NSLog(@"%@",bobo);

字典简介:

字典类用于保存具有映射关系(key-value对)的数据

一个key-value对认为是一个元素(实体),字典是存贮key-value对的容器

字典类的特点

于数组不同,数组靠下标存取数据,数据的下标是唯一的

字典靠key存取元素,key不能重复(如果重复只输出第一个,而第二个没有实际意义),value必须是对象

键值对在字典中是无序存储的,,,,

NSDictionary

不可变字典

字典一旦创建,键值对就不可更改,不可添加,不可删除

仅能读取key或者value