NSDictionary 使用总结:
1.工厂方法创建字典:
#pragma mark - 创建字典(+)
-(void)Creating
{
//空字典
NSDictionary *dictionary = [NSDictionary dictionary];
// 测试数据
NSString *filePath = [self testData];
// 通过文件路径创建字典
dictionary = [NSDictionary dictionaryWithContentsOfFile:filePath];
dictionary = [NSDictionary dictionaryWithContentsOfURL:[NSURL fileURLWithPath:filePath]];
//通过字典生成一个新的字典
dictionary = [NSDictionary dictionaryWithDictionary:dictionary];
// 生成只有一个键-值对的字典
dictionary = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];
// 根据两个数组合并生成包含多个键-值对的字典
NSArray *values = [NSArray arrayWithObjects:@"Hello",@"World",nil];
NSArray *keys = [NSArray arrayWithObjects:@"key1",@"key2",nil];
dictionary = [NSDictionary dictionaryWithObjects:values forKeys:keys];
// 生成包含多个键-值对的字典。数据的顺序为value,key,nil
dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Hello",@"key1",@"World",@"Key2", nil];
dictionary = @{@"key1":@"Hello",@"key2":@"World"};
}
2.初始化创建字典
-(void)Initializing
{
// 空字典
NSDictionary *dictionary = [[NSDictionary alloc] init];
// 测试数据
NSString *filePath = [self testData];
// 通过文件路径创建字典
dictionary = [[NSDictionary alloc] initWithContentsOfURL:[NSURL fileURLWithPath:filePath]];
dictionary = [[NSDictionary alloc] initWithContentsOfFile:filePath];
// 通过字典生成一个新的字典
dictionary = [[NSDictionary alloc] initWithDictionary:dictionary];
dictionary = [[NSDictionary alloc] initWithDictionary:dictionary copyItems:YES];
// 根据两个数组合并生成包含多个键-值对的字典
NSArray *values = [NSArray arrayWithObjects:@"Hello", @"World", nil];
NSArray *keys = [NSArray arrayWithObjects:@"key1", @"key2", nil];
dictionary = [[NSDictionary alloc] initWithObjects:values forKeys:keys];
// 生成包含多个键-值对的字典。数据的顺序为value,key,nil
dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"Hello", @"key1", @"World", @"key2", nil];
}
3.计算个数:
#pragma mark 计算个数
-(void)CountingEntries
{
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Hello",@"Key1",@"World",@"Key2", nil];
// 字典内的key-value个数,
NSUInteger count = dictionary.count;
NSLog(@"count:%lu",(unsigned long)count);
}
4.比较:
#pragma mark 比较
-(void)ComparingDictionaries
{
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Hello",@"Key1",@"World",@"Key2",nil];
NSDictionary *dictionary1 = [NSDictionary dictionaryWithObjectsAndKeys:@"Hello",@"Key1",@"World",@"Key2",nil];
// 比较字典中的数据是否一致
BOOL isEqual = [dictionary isEqualToDictionary:dictionary1];
NSLog(@"isEqualToDictionary:%d", isEqual);// out 1
}
5.访问键和值
#pragma mark 访问键和值
-(void)AccessKeysAndValues
{
NSDictionary *dictionary = @{@"Key1":@"Hello",@"Key2":@"World"};
// 所有的keys
NSArray *keys = dictionary.allKeys;
NSLog(@"keys:%@",keys);
// 根据value获取keys,可能多个key指向
keys = [dictionary allKeysForObject:@"Hello"];
// 所有的values
NSArray *values = dictionary.allValues;
NSLog(@"values:%@",values);
// 根据key提取value
NSString *value = [dictionary objectForKey:@"Key1"];
value = [dictionary valueForKey:@"Key2"];
value = [dictionary objectForKeyedSubscript:@"Key1"];
// 根据多个key获取多个value,如果没找到
values = [dictionary objectsForKeys:keys notFoundMarker:@"NotFound"];
}
6.遍历
#pragma mark 遍历
-(void)Enumerating
{
NSDictionary *dictionary = @{@"Key1":@"Hello",@"Key2":@"World"};
//遍历keys
NSEnumerator *enumerator = [dictionary keyEnumerator];
id key;
while (key = [enumerator nextObject]) {
NSLog(@"key:%@",key);
}
// 遍历values
enumerator = [dictionary objectEnumerator];
id value;
while (value = [enumerator nextObject]) {
NSLog(@"value:%@",value);
}
// 快速遍历key,value
for (id key in dictionary) {
id value = [dictionary objectForKey:key];
NSLog(@"%@=%@", key, value);
}
// key-value遍历
[dictionary enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSLog(@"key:%@; value:%@", key, obj);
*stop = YES;// 当stop设为YES时,会停止遍历
}];
[dictionary enumerateKeysAndObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSLog(@"key:%@; value:%@", key, obj);
// *stop = YES;// 当stop设为YES时,会停止遍历,必须设NSEnumerationReverse
}];
}
7.排序
#pragma mark 排序
-(void)Sorting
{
NSDictionary *dictionary = @{@"Key1":@"Hello",@"Key2":@"World"};
// 使用Selector比较获取key, compare:由对象去实现
NSArray *array = [dictionary keysSortedByValueUsingSelector:@selector(compare:)];
// 使用block比较value,排序key
array = [dictionary keysSortedByValueUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return [obj1 compare:obj2];
}];
array = [dictionary keysSortedByValueWithOptions:NSSortStable usingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return [obj1 compare:obj2];
}];
}
8.过滤
#pragma mark 过滤
-(void)Filtering
{
NSDictionary *dictionary = @{@"Key1":@"Hello",@"Key2":@"World"};
// 返回过滤后的keys
NSSet *set = [dictionary keysOfEntriesPassingTest:^BOOL(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
// stop 是否停止过滤
// return 通过yes,禁止no
return YES;
}];
// 多核过滤
set = [dictionary keysOfEntriesWithOptions:NSEnumerationConcurrent passingTest:^BOOL(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
// stop 是否停止过滤
// return 通过yes,禁止no
return YES;
}];
}
9.存储到文件
-(void)Storing
{
NSDictionary *dictionary = @{@"Key1":@"Hello",@"Key2":@"World"};
NSString *filePath = [self testData];
// 根据路径存储字典
BOOL write = [dictionary writeToFile:filePath atomically:YES];
write = [dictionary writeToURL:[NSURL fileURLWithPath:filePath] atomically:YES];
[dictionary fileCreationDate];
}
10.访问文件属性
#pragma mark 访问文件属性
- (void)AccessingFileAttributes {
NSString *filePath = [self testData];
NSError *error;
NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&error];
NSLog(@"创建时间:%@", dictionary.fileCreationDate);
NSLog(@"是否可见:%d", dictionary.fileExtensionHidden);
NSLog(@"组ID:%@", dictionary.fileGroupOwnerAccountID);
NSLog(@"组名:%@", [dictionary fileGroupOwnerAccountName]);
NSLog(@"HFS编码:%u", (unsigned int)dictionary.fileHFSCreatorCode);
NSLog(@"HFS类型编码:%u", (unsigned int)dictionary.fileHFSTypeCode);
NSLog(@"是否只读:%d", dictionary.fileIsAppendOnly);
NSLog(@"是否可修改:%d", dictionary.fileIsImmutable);
NSLog(@"修改时间:%@", dictionary.fileModificationDate);
NSLog(@"所有者ID:%@", dictionary.fileOwnerAccountID);
NSLog(@"所有者名:%@", dictionary.fileOwnerAccountName);
NSLog(@"Posix权限:%lu", (unsigned long)dictionary.filePosixPermissions);
NSLog(@"大小:%llu", dictionary.fileSize);
NSLog(@"系统文件数量:%lu", (unsigned long)dictionary.fileSystemFileNumber);
NSLog(@"文件系统的数量:%ld", (long)dictionary.fileSystemNumber);
NSLog(@"文件类型:%@", dictionary.fileType);
}
11.可变字典初始化:
#pragma mark 初始化
-(void)CreatingAndInitializing
{
// 创建包含一个key-value的可变字典。
NSMutableDictionary *mDictionary = [NSMutableDictionary dictionaryWithCapacity:1];
mDictionary = [[NSMutableDictionary alloc] initWithCapacity:1];
// 空字典
mDictionary = [NSMutableDictionary dictionary];
mDictionary = [[NSMutableDictionary alloc] init];
}
12.可变字典添加元素
-(void)AddingEntries
{
NSMutableDictionary *mDictionary = [NSMutableDictionary dictionary];
//增加单一记录
[mDictionary setObject:@"Hello" forKey:@"Key1"];
[mDictionary setObject:@"World" forKeyedSubscript:@"Key2"];
[mDictionary setValue:@"Lucy" forKey:@"Key3"];
NSLog(@"%@",mDictionary);
// 从字典中增加数据
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:@"IOS" forKey:@"Key4"];
[mDictionary addEntriesFromDictionary:dictionary];
NSLog(@"%@",mDictionary);
// 用新的字典数据覆盖原有字典数据
[mDictionary setDictionary:dictionary];
NSLog(@"%@",mDictionary);
}
13.可变字典删除元素
#pragma mark 删除记录
-(void)RemovingEntries
{
NSMutableDictionary *mDictionary = [NSMutableDictionary dictionary];
//增加单一记录
[mDictionary setObject:@"Hello" forKey:@"Key1"];
[mDictionary setObject:@"World" forKeyedSubscript:@"Key2"];
[mDictionary setValue:@"Lucy" forKey:@"Key3"];
[mDictionary setValue:@"IOS" forKey:@"Key4"];
NSLog(@"%@",mDictionary);
// 根据key删除单一记录
[mDictionary removeObjectForKey:@"Key1"];
NSLog(@"%@",mDictionary);
// 批量删除多个key对应的记录
NSArray *keys = [NSArray arrayWithObjects:@"Key2",@"Key3",nil];
[mDictionary removeObjectsForKeys:keys];
NSLog(@"%@",mDictionary);
// 删除所有记录
[mDictionary removeAllObjects];
NSLog(@"%@",mDictionary);
}
demo地址:demo