<span style="font-size:24px;"> NSLog(@"-------------NSDictionary-------------");
//1.创建字典
NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:@"小明",@"name",@"11",@"number",@(100),@"score",nil];
NSDictionary *dict2 = @{
@"name":@"小红",
@"number":@"12",
@"height":@(168)
};
NSLog(@"%@\n%@",dict1,dict2);
//1-1根据现有的字典创建字典
NSDictionary *dict3 = [NSDictionary dictionaryWithDictionary:dict1];
NSLog(@"根据现有的字典dict1创建字典dict3 :%@", dict3);
NSLog(@"---------------------------");
//2.得到字典的数量
NSLog(@"字典dict1的数量%lu",(unsigned long)dict1.count);
NSLog(@"字典dict2的数量%lu",(unsigned long)dict2.count);
NSLog(@"---------------------------");
//3.得到字典中所有KEY值
NSEnumerator *enumeratorKey1 = [dict1 keyEnumerator];
//3-1.快速枚举遍历所有KEY的值
for (NSObject *object in enumeratorKey1) {
NSLog(@"字典dict1所有KEY值: %@",object);
}
//3-2.所有KEY的集合
NSArray *key1s = [dict1 allKeys];
NSLog(@"字典1所有KEY的集合keys :%@", key1s);
NSLog(@"---------------------------");
//4.得到字典中所有value值
NSEnumerator *enumeratorValue1 = [dict1 objectEnumerator];
//4-1.快速枚举遍历所有value值
for (NSObject *object in enumeratorValue1) {
NSLog(@"字典dict1所有value值: %@",object);
}
//4-2.所有值集合
NSArray *values = [dict1 allValues];
NSLog(@"字典dict1所有value值集合 :%@", values);
NSLog(@"---------------------------");
//5.通过KEY找到value
NSObject *object = [dict1 valueForKey:@"score"];
if (object) {
NSLog(@"通过KEY找到的value是: %@",object);
}
NSLog(@"---------------------------");
NSLog(@"-------------NSMutalbeDictionary-------------");
//1.创建可变字典
NSMutableDictionary *mutableDict1 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
@(180), @"height",
@"小陈", @"name", nil];
//2.可变字典mutableDict1添加现有的字典数据dict1
[mutableDict1 addEntriesFromDictionary:dict1];
NSLog(@"添加之前--原来字典dict1%@",dict1);
NSLog(@"添加之后--可变字典mutableDict1添加现有的字典数据dict1%@",mutableDict1);
NSLog(@"结论:可变字典添加字典后,两个字典若有相同的key,后来的key对应的value会覆盖原来的value值");
//3.根据key删除value
[mutableDict1 removeObjectForKey:@"height"];
NSLog(@"删除KEYheight后,可变字典mutableDict1 :%@",mutableDict1);
//4.添加新的键值对象
[mutableDict1 setObject:@NO forKey:@"sex"];
NSLog(@"添加新的键值对,可变字典mutableDict1 :%@",mutableDict1);
//5.description
NSLog(@"描述description:%@",mutableDict1.description);
NSLog(@"---------------------------");
//6.字典写到文件去
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) firstObject];
NSString *fileName = @"customFile";//文件名
NSString *filePath = [path stringByAppendingString:fileName];
BOOL result = [mutableDict1 writeToFile:filePath atomically:YES];
if (result) {
NSLog(@"mutableDict1字典,成功写入到文件中");
NSLog(@"保存的路径filePath:%@",filePath);
}
NSLog(@"---------------------------");
//7.取出保存在本地文件中的字典
NSMutableDictionary *getMutabeDict = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
NSLog(@"取出保存在本地文件中的字典:getMutabeDict%@",getMutabeDict);
NSLog(@"---------------------------");
//8.使用block遍历所有的键值对
[mutableDict1 enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(@"使用block遍历所有的键值对\n%@:%@",key,obj);
}];
</span>
iOS笔记 -(NSDictionary和NSMutableDictionary常用方法总结)
最新推荐文章于 2020-12-19 07:35:40 发布