首页 > 系统 > iOS > 正文

IOS开发之OC篇(4)—— NSDictionary和NSMutableDictionary

2019-11-09 14:30:55
字体:
来源:转载
供稿:网友

版本

Xcode 8.2.1

一、不可变字典NSDictionary

字典就是,key和value成对相对应地存入dictionary。key为字符串,value为所有对象Object,可以是string、array等等。

    //创建:value1,key1,value2,key2...,(key和value成对出现)

    //value:OC的任何对象都可以,

    NSDictionary *dic11 = [NSDictionary dictionaryWithObjectsAndKeys:@"aabbb",@"1",@"dddeee",@"2",nil];

    NSLog(@"dic = %@",dic11);

    

    //key1:value1,key2:value2...

    //@{}只能用于创建不可变字典

    NSDictionary *dic1 = @{@"One":@"11",@"Two":@"22",@"Three":@"22"};

    NSLog(@"dic1 = %@",dic1);

    

    //通过key值访问value值

    NSString *strDic = [dic1 valueForKey:@"Two"];

    NSLog(@"strDic = %@",strDic);

    

    //访问value,dic [key]

    NSString *strDic1 = dic1[@"Two"];

    NSLog(@"strDic1 = %@",strDic1);

    

    //遍历字典,遍历key值

    NSArray *keyArr = [dic1 allKeys];

    for(NSString *keyin keyArr) {

        NSLog(@"key = %@",key);

    }

    

    //通过value,获取具有相同value的key array

    NSArray *keyArr1 = [dic1 allKeysForObject:@"22"];

    

    //遍历Value值,返回所有的Value值

    NSArray *valArr = [dic1 allValues];

    for(id valuein valArr) {

        NSLog(@"value = %@",value);

    }

    

    //默认遍历key值

    for(id objin dic1) {

        NSLog(@"obj = %@",dic1[obj]);

    }

二、可变字典NSMutableDictionary

//实例化一个空的可变字典

    NSMutableDictionary *dic2 = [NSMutableDictionary dictionary];

 //   NSMutableDictionary *dic2 = [[NSMutableDictionary alloc]init];

    

    //通过一个数组实例化一个可变字典

    NSMutableDictionary *dic3 = [NSMutableDictionary dictionaryWithObjects:@[@"1",@"2",@"3"] forKeys:@[@"One",@"Three",@"Three"]];

    NSLog(@"dic3 = %@",dic3);

    

    //添加元素(当字典内不存在此key值)

    [dic3 setObject:@"4" forKey:@"Four"];

    

    //修改value值(当字典内存在此key值)

    [dic3 setObject:@"11" forKey:@"One"];

    

    //通过key值获取value值

    NSString *rstr = [dic3 valueForKey:@"Two"];//dic3[@"Two"];

    

    //删除,删除键值对

    [dic3 removeObjectForKey:@"One"];

    

    //删除所有数组中含有的key值的键值对

    [dic3 removeObjectsForKeys:@[@"Four",@"Three"]];

    

    //删除所有键值对

    [dic3 removeAllObjects];

NSArray栗子:

先创建一个plist文件,并取名dict3,等下我们将通过这个名字来查找内容:

填入key和value:

然后来到main方法:

int main(int argc, char * argv[]) {        //@{}只能用于创建不可变字典(注意:先key后value)    NSDictionary *dict1 = @{@"role":@"Invincible Eastern",@"age":@"30",@"sex":@"male&female"};    NSLog(@"%@",dict1);        //value:OC的任何对象都可以(注意:先value后key)    NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:@"the monkey king",@"role",@"500",@"age",@"male",@"sex", nil];    NSLog(@"%@",dict2);        //查plist文件    NSDictionary *dict3 = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dict3" ofType:@"plist" ]];    NSLog(@"%@",dict3);    NSLog(@"%@",dict3[@"role"]);        //访问value    NSLog(@"%@",[dict2 valueForKey:@"age"]);    NSLog(@"%@",[dict2 objectForKey:@"age"]);    NSLog(@"%@",dict2[@"age"]);        //遍历value    NSArray *valArr = [dict2 allValues];    for(id value in valArr){        NSLog(@"value = %@",value);    }        //获取具有相同value的key array    NSArray *keyArr1 = [dict2 allKeysForObject:@"500"];    NSLog(@"500的key为%@",keyArr1);        //遍历key    NSArray *keyArr2 = [dict2 allKeys];    for(id value in keyArr2){        NSLog(@"key = %@",value);    }        //默认遍历keu    for(id value in dict2){        NSLog(@"obj = %@",dict2[value]);    }}结果如下:

NSMutableDictionary栗子:

int main(int argc, char * argv[]) {        //实例化一个可变字典    NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];    //NSMutableDictionary *dic1 = [[NSMutableDictionary alloc]init];    NSLog(@"dic1 = %@",dic1);        //通过一个数组实例化一个可变字典    NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithObjects:@[@"Invincible Eastem",@"30"] forKeys:@[@"role",@"age"]];    NSLog(@"dic2 = %@",dic2);        //添加元素(当字典内不存在此key值)    [dic2 setObject:@"male" forKey:@"sex"];    NSLog(@"sex = %@",dic2);        //修改value值(当字典内存在此key值)    [dic2 setObject:@"female" forKey:@"sex"];    NSLog(@"sex = %@",dic2);        //通过key值获取value值    NSLog(@"sex = %@",[dic2 valueForKey:@"sex"]);    NSLog(@"sex = %@",dic2[@"sex"]);        //删除键值对    [dic2 removeObjectForKey:@"role"];    [dic2 removeObjectsForKeys:@[@"role",@"age"]];        //删除所有键值对    [dic2 removeAllObjects];    NSLog(@"sex = %@",dic2);}输出结果:


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表