前言
@interface NSDictionary<__covariant KeyType, __covariant ObjectType> : NSObject <NSCopying, NSMutableCopying, NSSecureCoding, NSFastEnumeration>
@interface NSMutableDictionary<KeyType, ObjectType> : NSDictionary<KeyType, ObjectType>
字典只能存储 OC 对象,不能存储 C 语言中的基本数据类型,也不能存储 nil 。字典中的键必须是单值的,通常它们是字符串。
字典对象是无序的,第一个放到词典中的键-对象对并不一定是第一个取出。
-
Xcode 7 对系统中常用的一系列容器类型都增加了泛型支持(),有了泛型后就可以指定容器类中对象的类型了。假如向泛型容器中加入错误的对象,编译器会报警告。
__covariant:协变性,子类型可以强转到父类型(里氏替换原则)。 __contravariant:逆变性,父类型可以强转到子类型。
不指定泛型类型的对象可以和任意泛型类型转化,但指定了泛型类型后,两个不同类型间是不可以强转的,假如你希望主动控制转化关系,就需要使用泛型的协变性和逆变性修饰符。
1、不可变字典的创建
// 直接创建不可变字典
// 字典中的 key 和 value 成对出现
NSDictionary *dic1 = @{@"key1":@"value1", @"key2":@"value2"};
// 对象方法创建
NSDictionary *dic2 = [[NSDictionary alloc] init];
// 和 dic1 的地址不相同
NSDictionary *dic3 = [[NSDictionary alloc] initWithDictionary:dic1];
NSDictionary *dic4 = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];
NSDictionary *dic5 = [[NSDictionary alloc] initWithObjects:@[@"value1", @"value2", @"value3",]
forKeys:@[@"key1", @"key2", @"key3"]];
// 类方法创建
NSDictionary *dic6 = [NSDictionary dictionary];
// 和 dic1 的地址不相同
NSDictionary *dic7 = [NSDictionary dictionaryWithDictionary:dic1];
NSDictionary *dic8 = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];
NSDictionary *dic9 = [NSDictionary dictionaryWithObject:@"value1" forKey:@"key1"];
NSDictionary *dic10 = [NSDictionary dictionaryWithObjects:@[@"value1", @"value2", @"value3",]
forKeys:@[@"key1", @"key2", @"key3"]];
// 从 文件 创建字符串
NSString *path = [NSHomeDirectory() stringByAppendingString:@"/Desktop/test.txt"];
NSDictionary *dic11 = [[NSDictionary alloc] initWithContentsOfFile:path];
NSDictionary *dic12 = [NSDictionary dictionaryWithContentsOfFile:path];
// 从 Url 创建字符串
/*
file:// 文件前缀
*/
NSURL *url = [NSURL URLWithString:[@"file://" stringByAppendingString:[NSHomeDirectory()
stringByAppendingString:@"/Desktop/test.txt"]]];
NSDictionary *dic13 = [[NSDictionary alloc] initWithContentsOfURL:url];
NSDictionary *dic14 = [NSDictionary dictionaryWithContentsOfURL:url];
// 泛型创建
NSDictionary<NSString *, NSNumber *> *dic15 = @{@"key1":@3, @"key2":@5};
2、字典中元素个数的计算
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];
NSUInteger length = [dic count];
3、通过 key 找对应的 value
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];
// 通过 key 找对应的 value
NSLog(@"%@", [dic objectForKey:@"key1"]);
// 以下标的方式找 key 找对应的 value
NSLog(@"%@", dic[@"key2"]);
// key 没有对应的 value 时用 notFoundMarker 参数代替
NSArray *arr = [dic objectsForKeys:@[@"key1", @"key1", @"key3"] notFoundMarker:@"not-Found"];
4、通过 value 找对应的 key
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", @"value2",
@"key2", @"value2", @"key3", nil];
NSLog(@"%@", [dic allKeysForObject:@"value2"]);
5、字典中所有 key 的获取
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];
NSLog(@"%@", [dic allKeys]);
6、字典中所有 value 的获取
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];
NSLog(@"%@", [dic allValues]);
7、可变字典 NSMutableDictionary 的创建
// 对象方法创建
// 创建指定长度的字典,预先分配空间,提高效率,实际长度可大于指定长度
NSMutableDictionary *dic1 = [[NSMutableDictionary alloc] initWithCapacity:0];
// 类方法创建
// 创建指定长度的字典,预先分配空间,提高效率,实际长度可大于指定长度
NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithCapacity:0];
8、字典中键值对的追加
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", nil];
// 向字典末尾追加一个元素(key 值为新增的,若 key 值已存在则为修改对应的 value 值)
[dic setObject:@"value2" forKey:@"key2"];
// 向字典末尾追加一个字典
NSDictionary *dicAdd = @{@"key3": @"value3"};
[dic addEntriesFromDictionary:dicAdd];
9、字典中元素的删除
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", @"value2",
@"key2", @"value3", @"key3",
@"value4", @"key4", nil];
// 删除指定的 key 对应的 value 和 key 值
[dic removeObjectForKey:@"key3"];
// 删除所有的 value 和 key 值
[dic removeAllObjects];
10、字典中元素的修改
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", @"value2",
@"key2", @"value3", @"key3",
@"value4", @"key4", nil];
// 修改已存在的键值(key 值为已经存在的,若 key 值为新增的则为追加对应的 value 值)
[dic setObject:@"value5" forKey:@"key1"];
// 修改整个字典(覆盖重写)
NSDictionary *dic1 = @{@"key10":@"value10"};
[dic setDictionary:dic1];
11、字典遍历
-
for 循环
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil]; for (int i = 0; i < [dic count]; i++) { NSLog(@"%@ = %@", [[dic allKeys] objectAtIndex:i], [dic objectForKey:[[dic allKeys] objectAtIndex:i]]); }
-
for...in 循环
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil]; // 遍历所取出的为 key for(id key in dic){ NSLog(@"%@ = %@", key, [dic objectForKey:key]); }
-
Block 遍历
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil]; [dic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) { NSLog(@"%@ = %@", key, obj); }];
-
迭代器遍历
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil]; // key 迭代器 NSEnumerator *keyEnumer = [dic keyEnumerator]; id key = nil; while (key = [keyEnumer nextObject]) { NSLog(@"%@ = %@", key, [dic objectForKey:key]); } // 对象迭代器 NSEnumerator *objEnumer = [dic objectEnumerator]; id obj = nil; while (obj = [objEnumer nextObject]) { NSLog(@"%@", obj); }
12、字典排序
-
用指定的方法排序
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", @"value3", @"key3", @"value4", @"key4", nil]; // 先对 key 进行排序,再遍历数组 id keys1 = [dic keysSortedByValueUsingSelector:@selector(compare:)]; for (id key in keys1) { NSLog(@"%@ = %@", key, dic[key]); }
-
用 Block 排序
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", @"value3", @"key3", @"value4", @"key4", nil]; id keys2 = [dic keysSortedByValueUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) { return [obj1 compare:obj2]; }]; for (id key in keys2) { NSLog(@"%@ = %@", key, dic[key]); }