Objective-C中关于NSDictionary的一些常用的操作方法
来源:互联网 发布:打补丁用什么软件 编辑:程序博客网 时间:2024/05/14 17:29
//类方法 定义字典对象
NSDictionary *classDic = [NSDictionary dictionaryWithObjectsAndKeys:
@"1", @"one",
@"2", @"two",
@"3", @"three", nil];
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:
@"1", @"one",
@"2", @"two",
@"3", @"three",
nil];
NSLog(@"dictionary %@", dic);
//实例方法
NSArray *keyArray = [[NSArray alloc] initWithObjects:
@"one", @"two", @"three", nil];
NSArray *valueArray = [[NSArray alloc] initWithObjects:
@"1", @"2", @"3", nil];
NSDictionary *dic2 = [[NSDictionary alloc] initWithObjects: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 alloc] initWithObjectsAndKeys:@"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
- Objective-C中关于NSDictionary的一些常用的操作方法
- objective-c 中NSDictionary的使用
- Objective-C 集合之NSDictionary的常用方法
- Objective-c的Foundation中常用类4——字典NSDictionary、NSMutableDictionary
- Objective-C中关于NSArray, NSDictionary, NSNumber等写法的进化
- Objective-C中关于请求返回NSData数据解析成NSDictionary或NSArray的方法
- 关于Java中数组的常用操作方法
- Objective-C中NSDictionary和NSMutableDictionary的基本用法
- objective-c 中具有对象的类的操作方法
- 关于Objective-C中常用的操作字符串的方法
- objective-c中关于协议protocol的一些理解
- Objective-C的字典类NSDictionary
- Objective-C中关于数组操作常用的方法
- objective-c 常用的一些控件。
- 关于Objective-C的一些基本知识
- C#常用的一些简单的操作方法
- C#的一些常用文件操作方法[极好]
- C#的一些常用文件操作方法[极好]
- 协方差的意义和计算公式
- C#获取电脑名IP地址及当前用户名
- 关闭浏览器 弹窗口
- IOS Info.plist中常用的key
- ios-实现9宫格
- Objective-C中关于NSDictionary的一些常用的操作方法
- RHEL下Qt4.7.4安装
- 多线程和异步
- linux swap 空间
- Android有用代码片段(二)
- mysql 创建外键的问题 error 1005 errno 150 工具navicat
- Win7下VS2010不能链接问题
- Python中使用中文--SyntaxError: Non-ASCII character
- 进程、会话、连接之间的差异 .+数据库资料