OC 自带方法 简绍 5 NSDictionary
来源:互联网 发布:房地产数据来源 编辑:程序博客网 时间:2024/05/02 00:21
1.字典
initWithObjectsAndKeys ; 初始化字典
dictionaryWithObjectsAndKeys : 初始化字典
dictionaryWithObjects forKeys:
1.1
NSDictionary * dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"m", @"sex", @"25", @"age", @"zhangsan", @"name",nil];
NSLog(@"%@", dic);
1.2
NSDictionary * dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan", @"name", @"21", @"age",nil];
NSLog(@"%@", dic2);
1.3
NSArray * valueArray = [NSArray arrayWithObjects:@"fulushou", @"M", @"25", nil];
NSArray * keyArray = [NSArray arrayWithObjects:@"name", @"sex", @"age", nil];
NSDictionary * dic = [NSDictionary dictionaryWithObjects:valueArray forKeys:keyArray];
NSLog(@"%@", dic);
2.//count // allKeys //allValues // 对数 , 所有的键 , 所有的值
@property(readonly) NSUInteger count //The number of entries in the dictionary (read-only)
@property(readonly, copy) NSArray *allKeys //A new array containing the dictionary’s keys, or an empty array if the dictionary has no entries (read-only)
例子
NSLog(@"%lu", [dic count]);
NSLog(@"%@", [dic allKeys]);
NSLog(@"%@", [dic allValues]);
3.objectForKey 通过键获取值字典中的值
- (id)objectForKey:(NSString *)key // Returns the value for the given key stored in the record.
例子
NSString * str = [dic objectForKey:@"name"];
NSLog(@"%@", str);
// 怎么遍历 字典 ? 方法一
for (int i = 0; i < [dic count]; i++) {
NSString * value = [[dic allKeys] objectAtIndex:i];
NSLog(@"%@", [dic objectForKey:value]);
}
for (int i = 0; i < [dic count]; i++) {
NSString * value = [[dic allValues] objectAtIndex:i];
NSLog(@"%@", value);
}
4.NSMutableDictionary //继承自Dictdonary 的可改变的字典
NSMutableDictionary * dic = [NSMutableDictionary dictionaryWithCapacity:1];
5.setObject //给字典添加数据
virtual bool setObject( const char *aKey, const OSMetaClassBase *anObject); //Stores an object in the dictionary under a key.
例子
// [dic setObject:@"fulushou" forKey:@"name"];
// [dic setObject:@"M" forKey:@"sex"];
// [dic setObject:@"25" forKey:@"age"];
6.removeObjectForKey //通过key 删除字典中数据
- (void)removeObjectForKey:(NSString *)defaultName //Removes the value of the specified default key in the standard application domain.
例子
[dic removeObjectForKey:@"age2"];
NSLog(@"%@", dic);
7.arrayWithObjects //批量删除字典中数据
+ (instancetype)arrayWithObjects:(id)firstObj , ... //Creates and returns an array containing the objects in the argument list.
NSArray * arr = [NSArray arrayWithObjects:@"age", @"sex", nil];
[dic removeObjectsForKeys:arr];
NSLog(@"%@", dic);
8.removeAllObjects //清空字典
- (void)removeAllObjects // Empties the cache.
例子
[dic removeAllObjects];
NSLog(@"%@", dic);
- OC 自带方法 简绍 5 NSDictionary
- OC 自带方法 简绍 1 NSString
- OC 自带方法 简绍 2 NSMutableString
- OC 自带方法 简绍 3 NSArray
- OC 自带方法 简绍 4 NSMutableArray
- OC 自带方法 简绍 6 NSSet
- OC 自带排序方法
- OC NSDictionary 的常用方法
- NSJSON的用法(oc系统自带的解析方法)
- OC中NSDictionary常用方法总结
- [OC学习笔记]NSDictionary、NSMutableDictionary常用方法
- OC中NSDictionary、NSSet得常用方法
- OC-NSDictionary
- OC-NSDictionary
- 14-OC中NSDictionary常用方法,遍历,读写文件
- OC关于NSDictionary和NSMutableDictionary方法的使用
- OC学习:OC字典NSDictionary
- OC语法之NSDictionary
- Tomcat 双向证书配置
- 游戏中如何实现GameCenter的应用
- 电脑文件整理思路
- 堆和栈的区别
- HTML5 零一
- OC 自带方法 简绍 5 NSDictionary
- 常用CSS属性
- 浏览器各种奇葩劫持,怎么破?
- android Gps 定位方法详解
- nginx fastcgi_params与fastcgi.conf
- Win8创建wifi热点
- 【第13篇】通过GSON的JsonParser去读取数据处理json数据
- Openfire服务器的安装部署详细步骤
- 中国信息安全发展是互联网发展重点