iOS开发之OC篇(4)—— NSDictionary、NSMutableDictionary

版本

Xcode 8.2.1

一、不可变字典NSDictionary

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


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

    //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,获取具有相同valuekey 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);
}
输出结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值