Objective-C中关于NSDictionary的一些常用的操作方法

来源:互联网 发布:打补丁用什么软件 编辑:程序博客网 时间:2024/05/14 17:29

 //类方法 定义字典对象

        NSDictionary *classDic = [NSDictionary dictionaryWithObjectsAndKeys:

                                  @"1"@"one",

                                  @"2"@"two",

                                  @"3"@"three"nil];

        NSDictionary *dic = [[NSDictionary allocinitWithObjectsAndKeys:

                             @"1"@"one",

                             @"2"@"two",

                             @"3"@"three",

                             nil];

        NSLog(@"dictionary %@", dic);

        

        //实例方法

        NSArray *keyArray = [[NSArray allocinitWithObjects:

                             @"one"@"two"@"three"nil];

        NSArray *valueArray = [[NSArray allocinitWithObjects:

                               @"1"@"2"@"3"nil];

        NSDictionary *dic2 = [[NSDictionary allocinitWithObjects:valueArray forKeys:keyArray];

        NSLog(@"dic2 %@", dic2);

        

        //获得字典键值对的个数

        NSLog(@"number of dictionary %ld", [dic2 count]);

        

        //获取字典里面所有的键

        NSArray *keysArray = [dic2 allKeys];

        NSLog(@"all keys %@", keysArray);

        

        //获取字典里面所有的对象(value

        NSArray *valuesArray = [dic2 allValues];

        NSLog(@"all values %@", valuesArray);

        

        

        //根据某个key获取其相对应的value

        NSString *value = [dic2 valueForKey:@"one"];

        NSLog(@"value for key(one) is %@", value);

        

        //获取对象所对应的key

        NSArray *keys = [dic2 allKeysForObject:@"1"];

        NSLog(@"keys for @\"1\" :%@", keys);

        

        //可变字典

        NSMutableDictionary *mutableDic = [[NSMutableDictionary allocinitWithObjectsAndKeys:@"hello",@"1",@"world",@"2"nil];

        [mutableDic setObject:@"zjy" forKey:@"zhongjunye"];

        NSLog(@"mutableDic %@",mutableDic);

        [mutableDic removeObjectForKey:@"1"];

        NSLog(@"mutableDic %@", mutableDic);

        

        NSNumber *num=[NSNumber numberWithBool:NO];

        [mutableDic setObject:@"YueZhuo" forKey:num];

        NSLog(@"mutableDic %@",mutableDic);


运行结果 :

2012-11-23 15:24:14.550 NSStringDemo[1420:403] dog id is 1

2012-11-23 15:24:14.554 NSStringDemo[1420:403] dog id is 2

2012-11-23 15:24:14.556 NSStringDemo[1420:403] dog id is 3

2012-11-23 15:24:14.557 NSStringDemo[1420:403] dog id is 4

2012-11-23 15:24:14.558 NSStringDemo[1420:403] dog id is 5

2012-11-23 15:24:14.560 NSStringDemo[1420:403] dog id is 6

2012-11-23 15:24:14.561 NSStringDemo[1420:403] dog id is 7

2012-11-23 15:24:14.563 NSStringDemo[1420:403] dog id is 8

2012-11-23 15:24:14.564 NSStringDemo[1420:403] dog id is 9

2012-11-23 15:24:14.565 NSStringDemo[1420:403] dog id is 10

2012-11-23 15:24:14.567 NSStringDemo[1420:403] dictionary {

    one = 1;

    three = 3;

    two = 2;

}

2012-11-23 15:24:14.569 NSStringDemo[1420:403] dic2 {

    one = 1;

    three = 3;

    two = 2;

}

2012-11-23 15:24:14.570 NSStringDemo[1420:403] number of dictionary 3

2012-11-23 15:24:14.571 NSStringDemo[1420:403] all keys (

    one,

    two,

    three

)

2012-11-23 15:24:14.572 NSStringDemo[1420:403] all values (

    1,

    2,

    3

)

2012-11-23 15:24:14.573 NSStringDemo[1420:403] value for key(one) is 1

2012-11-23 15:24:14.574 NSStringDemo[1420:403] keys for @"1" :(

    one

)

2012-11-23 15:24:14.575 NSStringDemo[1420:403] mutableDic {

    1 = hello;

    2 = world;

    zhongjunye = zjy;

}

2012-11-23 15:24:14.577 NSStringDemo[1420:403] mutableDic {

    2 = world;

   zhongjunye = zjy;

}

2012-11-23 15:24:14.578 NSStringDemo[1420:403] mutableDic {

    0 = YueZhuo;

    2 = world;

   zhongjunye = zjy;

}


欢迎加入iOS技术交流QQ群:160579517