NSDictionary的功能和用法

NSDictionary集合由多组key-value组成,因此创建NSDictionary时需要同时指定多组key-value对。 NSDICTIONARY分别提供了类方法和实例方法创建NSDictionary。

dictionary:创建一个不包含任何key-value对的NSDictionary。dictionaryWithContentsOfFile:/initWithContentsOfFile:读取指定文件的内容,使用指定文件内容来初始化NSDictionary。dictionaryWithObject:forKey:使用单个key-value来创建NSDictionary对象dictionaryWithObjects:forKeys:/initWithObjects:forKeys:使用两个NSArray分别指定Key,vakue集合,可以创建包含多组key-value对的NSDictionary。dictionaryWithObjectsAndKeys:调用该方法时,需要按value1, key1, value2, key2, …nil的方式传入多个key-value对。

一旦得到NSDictionary对象,接下来就可以通过方法访问该集合所包含的 key或value。

count: 该方法返回NSDictionary所包含的key-value对的数量。allKeys: 该方法返回该NSDictionary所包含的全部Key。allKeyForObject: 该方法返回指定value对应的全部Key。allValue: 该方法返回该NSDictionary所包含的全部value。objectForKey: 该方法获取该NSDictionary中指定Key对应的value。objectForKeyedSubScript: 通过该方法的支持,允许NSDictionary通过下标来获取指定key对应的value。valueForKey: 该方法获取该NSDictionary中指定Key对应的value。keyEnumerator: 该方法返回用于遍历该NSDicyoinary所有Key的NSEnumerator对象。enumerateKeysAndObjectsUsingBlock: 使用指定的代码块来迭代执行该集合中所有的key-value对。enumerateKeysAndObjectsWithOptions:usingBlock: 使用指定的代码块来迭代执行该集合中所有的key-value对。该方法可以传入一个NSEnumerationOptions参数。writeToFile: 将NSDictionary对象的数据写入指定文件。

下面为NSDictionary扩展一个print类别。

#import

@interface NSDictionary (print)

- (void) print;

@end

@implementation NSDictionary (print)

- (void) print {

NSMutableString* result = [NSMutableString stringWithString:@"{"];

for (id key in self) {

[result appendString:[key description]];

[result appendString:@"="];

[result appendString:[self[key] description]];

[result appendString:@", "];

}

NSUInteger len = [result length];

[result deleteCharactersInRange:NSMakeRange(len - 2, 2)];

[result appendString:@"}"];

NSLog(@"%@", result);

}

@end

上面的程序示范了NSDictionary的两个基本用法,程序可使用快速枚举来遍历NSDictionary的所有key。也可以根据key来获取对应NSDictionary中的value。

对NSDictionary的key排序

NSDictionary还提供了方法对NSDictionary的key执行排序,这些方法执行完成后将返回排序完成后的所有key组成的NSArray。

keysSortedByValueUsingSelector: 根据NSDictionary的所有value的指定方法的返回值对key排序;调用value的该方法必须返回NSOrderedAscending、NSOrderedDescending、NSOrderedSame这三个值之一。keySortedByValueUsingComparator: 该方法使用指定的代码块来遍历key-value对,并根据执行结果(执行结果必须返回NSOrderedAscending、NSOrderedDescending、NSOrderedSame这三个值之一)对NSDictionary的key进行排序。keySortedByValueWithOptions:usingComparator: 与前一个方法的功能类似,只是该方法可以传入一个额外的NSEnumerationOptions参数。

下面程序将示范使用代码块排序

#import

#import "NSDictionary+print.h"

int main(void) {

@autoreleasepool {

NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:@"Objective-c", @"one", @"Rudy", @"two", @"Python", @"three", nil];

// 打印dict集合所有元素

[dict print];

NSArray* keyArr = [dict keysSortedByValueUsingComparator:

^(id value1, id value2) {

if ([value1 length] > [value2 length]) {

return NSOrderedDescending;

}

if ([value1 length] < [value2 length]) {

return NSOrderedAscending;

}

return NSOrderedSame;

}];

NSLog(@"%@", keyArr);

}

}

代码块中的比较规则是,value对应的字符串越长,系统就认为该value值越大。

编译,运行该程序,即可看到如下输出:

2022-06-12 15:51:36.930684+0800 oc.programme[94349:95375889] {one=Objective-c, two=Rudy, three=Python}

2022-06-12 15:51:36.931119+0800 oc.programme[94349:95375889] (

two,

three,

one

)

Program ended with exit code: 0

对NSDicttionary的key进行过滤

NSDictionary还提供方法对所有的key执行过滤,这些方法返回满足过滤条件的key组成的NSSet。

keysOfEntriesPassingTest: 使用代码块迭代处理NSDictionary的每个key-value对。该代码块必须返回BOOL类型的值。该代码块可以接受三个参数,第一个参数代表正在迭代处理的key,第二个参数代表正在迭代处理的value,第三个参数代表时是否还需要继续迭代。keysOfEntriesWithOptions:passingTest: 该方法与前一个方法的功能基本相似。只是该方法可以额外传入一个附加的NSEnumerationOptions选项参数。

如下程序示范:

int main(void) {

@autoreleasepool {

NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:

[NSNumber numberWithInt:89], @"Objective-C",

[NSNumber numberWithInt:69], @"Rudy",

[NSNumber numberWithInt:75], @"Python",

[NSNumber numberWithInt:109], @"Perl", nil];

// 打印dict集合的所有元素

[dict print];

// 对NSDictionary的所有key进行过滤

NSSet* keySet = [dict keysOfEntriesPassingTest:

^(id key, id value, BOOL* stop) {

// 当value的值大于80时才返回YES

return (BOOL)([value intValue] > 80);

}];

NSLog(@"%@", keySet);

}

}

编译运行该程序,可以看到如下输出:

2022-06-12 16:22:34.784943+0800 oc.programme[4789:101091183] {Perl=109, Rudy=69, Objective-C=89, Python=75}

2022-06-12 16:22:34.785305+0800 oc.programme[4789:101091183] {(

Perl,

"Objective-C"

)}

Program ended with exit code: 0

NSMutableDictionary 的功能和用法。

NSMutableDictionary继承NSDictionary,它代表一个key-value可变的NSDictionary集合。

NSMutableDictionary主要新增了如下方法:

setObject:forKey: 设置一个key-value对。如果NSMutableDictionary中没有包含与该key相同的key-value,NSMutableDictionary将会新增一个key-value对;否则该key-value对将会覆盖已有的key-value对。setObject:forKeyedSubscript: 通过该方法支持,允许程序通过下标设置key-value对。addEntruesFromDictionary: 将另一个 NSDictionary中所有的key-value对复制到当前NSDictionary中。setDictionary: 将另一个 NSDictionary中所有的key-value对替换当前NSDictionary的key-value对。removeObjectForKey: 根据key来删除key-value对。removeAllObject: 清空该NSDictionary。removeObjectsForKeys: 使用多个key组成的NSArray作为参数,同时删除多个key对应的key-value对。

文章来源

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: