ios NSDictionary

来源:互联网 发布:mit 算法导论 pdf 编辑:程序博客网 时间:2024/05/29 09:26


NSDictionary:

NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:

                              @"value1", @"key1",
                              @"value2", @"key2",
                              @"value3", @"key3",
                              @"value4", @"key4",

                              nil];

     //所有的键集合
        NSArray *keys = [dic3 allKeys];
        NSLog(@"keys :%@", keys);
        
        //所有值集合
        NSArray *values = [dic3 allValues];
        NSLog(@"values :%@", values);



NSMutableDictionary

NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                           @"mvalue1", @"mkey1",
                                           @"mvalue2", @"mkey2", nil];
        //添加现有的字典数据
        [mutableDic addEntriesFromDictionary:dic3];
        NSLog(@"mutableDic :%@",mutableDic);
        
        //添加新的键值对象
        [mutableDic setValue:@"set1" forKey:@"setKey1"];
        NSLog(@"set value for key :%@",mutableDic);
        
        //以新的字典数据覆盖旧的字典数据
        [mutableDic setDictionary:dic2];
        NSLog(@" set dictionary :%@",mutableDic);
        
        //根据key删除value
        [mutableDic removeObjectForKey:@"key1"];
        NSLog(@"removeForkey :%@",mutableDic);

0 0
原创粉丝点击