IOS SDK详解之NSDictionary
原创Blog,转载请注明出处
blog.csdn.net/hello_hwc
前言:本文将要讲述的内容有
1.NSDictionary 以及 NSMutableDictionary 概述
2.常用属性方法举例(不常用的本文不会涉及)
一 NSDictionary/NSMutableDictionary概述
NSDictionary提供了一种key-value的数据存储方式。总的来说,任何对象都可以作为key,只要其遵循NSCopying协议。其中,key不能相同(由isEqual来判断)。key和value都不能为nil,如果要表达一个空的值,用NSNull。NSDictionary中的值不可变。
NSMutableDictionary是NSDictionary的子类,是可变的字典。
二 NSDictionary常用的属性方法举例
2.1 创建和初始化
创建兼初始化
(instancetype)dictionaryWithContentsOfFile:(NSString *)path
(instancetype)dictionary;
(instancetype)dictionaryWithDictionary:(NSDictionary *)
(instancetype)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys
(instancetype)dictionaryWithObjectsAndKeys:(id)firstObject
初始化
-(NSDictionary *)init;
-(NSDictionary *)initWithContentsOfFile:(NSString *)path;
-(NSDictionary *)initWithDictionary:(NSDictionary *);
-(NSDictionary *)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;
-(NSDictionary *)initWithObjectsAndKeys:(id)firstObject;
就个人而言,我比较习惯后一种。当然,快捷创建的方式不要忘记了
符号
@{}
举例:
NSDictionary * emptyDic = [NSDictionary dictionary];
NSDictionary * firstDic = @{@"key":@"value",
@"first":@"1"};
NSDictionary * secondDic = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1",@"key1",@"value2",@"key2",nil];
2.2 count
返回key-value对的个数
NSDictionary * dic = @{@"key1":@"1",
@"key2":@"2"};
NSLog(@"%d",dic.count);
2.3 isEqualToDictionary比较两个dictionary内容是否一样。
NSDictionary * dic1 = @{@"key1":@"1",
@"key2":@"2"};
NSDictionary * dic2 = @{@"key2":@"2",
@"key1":@"1"};
if ([dic1 isEqualToDictionary:dic2]) {
NSLog(@"Equal contents");
}
2.4 objectForKey: 和valueForKey 由属性获得内容
NSDictionary * dic1 = @{@"key1":@"1",
@"key2":@"2"};
NSLog(@"%@",[dic1 objectForKey:@"key1"]);
NSLog(@"%@",[dic1 valueForKey:@"key2"]);
2.5 allKeys 和 allValues 获得所有的key/value
NSDictionary * dic1 = @{@"key1":@"1",
@"key2":@"2"};
NSArray * keys = [dic1 allKeys];
NSArray * values = [dic1 allValues];
2.6 enumerateKeysAndObjectsUsingBlock 用Block的方式遍历
这里的stop决定了是否停止遍历。
NSDictionary * dic1 = @{@"key1":@"1",
@"key2":@"2"};
[dic1 enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(@"%@=>%@",[key description],[obj description]);
}];
2.7 排序
keysSortedByValueUsingSelector:
keysSortedByValueUsingComparator :
keysSortedByValueWithOptions: usingComparator:
返回Keys的数组,顺序按照value排序顺序。
NSDictionary * numsDic = @{@(2):@"second",
@(1):@"first",
@(3):@"thrid"};
NSDictionary * strDic = @{@"id_1":@"first",
@"id_3":@"thrid",
@"id_2":@"second"};
NSArray * numsSortedKeys = [numsDic keysSortedByValueUsingSelector:@selector(compare:)];
NSArray * strSortedKyes = [strDic keysSortedByValueUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSString * str1 = obj1;
NSString * str2 = obj2;
return [str1 compare:str2];
}];
NSLog(@"%@",numsSortedKeys.description);
NSLog(@"%@",strSortedKyes.description);
输出
2015-02-09 22:04:12.070 DictonaryExample[1037:23292] (
1,
2,
3
)
2015-02-09 22:04:12.070 DictonaryExample[1037:23292] (
“id_1”,
“id_2”,
“id_3”
)
2.8 过滤
keysOfEntriesPassingTest:
返回keys的集合,这些keys符合参数block的约束
NSDictionary * numsDic = @{@(2):@"second",
@(1):@"first",
@(3):@"thrid"};
NSSet * filteredKyes = [numsDic keysOfEntriesPassingTest:^BOOL(id key, id obj, BOOL *stop) {
BOOL result = NO;
NSNumber * numKey = key;
if (numKey.integerValue > 2) {
result = YES;
}
return result;
}];
NSLog(@"%@",filteredKyes.description);
输出
2015-02-09 22:09:50.800 DictonaryExample[1099:25241] {(
3
)}
2.9写到文件
writeToFile:atomically
writeToURL:atomically
NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] ;
NSString * fileName = @"file";
NSString * filePath = [path stringByAppendingPathComponent:fileName];
NSDictionary * dic = @{@"key":@"value"};
BOOL result = [dic writeToFile:filePath atomically:YES];
if (result) {
NSLog(@"Success");
}
2.10Description-常常用来debug输出dictionary的内容。
在之前已经举例好多了,这里不再赘述
三 NSMutableDictionary的额外方法
3.1 添加元素
- (void)setObject:(id)anObject
forKey:(id<NSCopying>)aKey
- (void)setValue:(id)value
forKey:(NSString *)key
- (void)setDictionary:(NSDictionary *)otherDictionary
注意,使用KVC的时候,key一定要是NSString。第三个函数是删除之前的元素,然后把otherDictionary元素放到当前dic中。
举例
NSMutableDictionary * dic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"object",@"key", nil];
NSLog(@"%@",dic.description);
[dic setDictionary:@{@"otherKey":@"otherValue"}];
NSLog(@"%@",dic.description);
输出
2015-02-09 22:31:21.417 DictonaryExample[1232:31666] {
key = object;
}
2015-02-09 22:31:21.418 DictonaryExample[1232:31666] {
otherKey = otherValue;
}
3.2删除元素
- (void)removeObjectForKey:(id)aKey
- (void)removeAllObjects
- (void)removeObjectsForKeys:(NSArray *)keyArray
比较容易忽视的是第三个,删除一组key。
举例
NSDictionary * dic = @{@(1):@"first",
@(2):@"second",
@(3):@"thrid"};
NSMutableDictionary * mutableDic = [[NSMutableDictionary alloc] initWithDictionary:dic];
[mutableDic removeObjectsForKeys:@[@(1),@(2)]];
NSLog(@"%@",mutableDic.description);
输出
2015-02-09 22:34:13.112 DictonaryExample[1273:32793] {
3 = thrid;
}
BTY:年前计划在更新一篇KVC 和 KVO的详细讲解。然后继续更新GCD系列的第五篇。如果精力够用的话,更再更新一篇Swift相关的。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。