1. NSDictionary
1.1 初始化方法
//直接赋值。
NSDictionary *dic = @{@"key-1":@"value-1", @"key-2":@"value-2"};
+ (instancetype)dictionary;//这个多用于创建无数据的可变字典。
+ (instancetype)dictionaryWithObject:(ObjectType)object forKey:(KeyType <NSCopying>)key;
NSDictionary *dic = [NSDictionary dictionaryWithObject:@"value-1" forKey:@"key-1"];
+ (instancetype)dictionaryWithDictionary:(NSDictionary<KeyType, ObjectType> *)dict;
+ (instancetype)dictionaryWithObjects:(NSArray<ObjectType> *)objects forKeys:(NSArray<KeyType <NSCopying>> *)keys;
NSDictionary *dic = [NSDictionary dictionaryWithObjects:@[@"value-1", @"value-2"] forKeys:@[@"key-1", @"key-2"]];
- (instancetype)initWithObjectsAndKeys:(id)firstObject, ... NS_REQUIRES_NIL_TERMINATION;
NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"value-1", @"key-1", @"value-2", @"key-2", nil];
- (instancetype)initWithDictionary:(NSDictionary<KeyType, ObjectType> *)otherDictionary;
- (instancetype)initWithObjects:(NSArray<ObjectType> *)objects forKeys:(NSArray<KeyType <NSCopying>> *)keys;
NSDictionary *dic = [[NSDictionary alloc]initWithObjects:@[@"value-1", @"value-2"] forKeys:@[@"key-1", @"key-2"]];
1.2 NSDictionary属性
//字典键值对的个数
NSUInteger count;
NSInteger count = dic.count;
//字典所有Key的数组
NSArray<KeyType> *allKeys;
NSArray *array = dic.allKeys;
//字典所有Value的数组
NSArray<ObjectType> *allValues;
NSArray *array = dic.allValues;
1.3 常用方法
//取值方法
NSString *str = dic[@"key-1"];//直接取值(常用)
- (nullable ObjectType)objectForKey:(KeyType)aKey;
NSString *str = [dic objectForKey:@"key-1"];
//枚举器,用于枚举器遍历方法
- (NSEnumerator<KeyType> *)keyEnumerator;//key枚举器
NSEnumerator *keyEnumerator = [dic keyEnumerator];
- (NSEnumerator<ObjectType> *)objectEnumerator;//object枚举器
NSEnumerator *objectEnumerator = [dic objectEnumerator];
//获取字典中anObject对应的所有key,字典中key是唯一的,但value不唯一,一个value可以对应多个key。
- (NSArray<KeyType> *)allKeysForObject:(ObjectType)anObject;
NSDictionary *dic = @{@"key-1":@"value-1", @"key-2":@"value-2", @"key-3":@"value-1"};
NSArray *array = [dic allKeysForObject:@"value-1"];
//array = @[@"key-1", @"key-3"];
//判断两个字典是否一样
- (BOOL)isEqualToDictionary:(NSDictionary<KeyType, ObjectType> *)otherDictionary;
2. NSMutableDictionary
2.1 常用方法
//添加键值对
- (void)setObject:(ObjectType)anObject forKey:(KeyType <NSCopying>)aKey;
[mutableDic setObject:@"value-1" forKey:@"key-1"];
//根据aKey删除键值对
- (void)removeObjectForKey:(KeyType)aKey;
[mutableDic removeObjectForKey:@"key-1"];
//删除所有数据
- (void)removeAllObjects;
[mutableDic removeAllObjects];
//根据keyArray删除多个键值对
- (void)removeObjectsForKeys:(NSArray<KeyType> *)keyArray;
[mutableDic removeObjectsForKeys:@[@"key-1", @"key-2"]];
//添加otherDictionary整个字典的数据
- (void)addEntriesFromDictionary:(NSDictionary<KeyType, ObjectType> *)otherDictionary;
[mutableDic addEntriesFromDictionary:dic];
//重新设置
- (void)setDictionary:(NSDictionary<KeyType, ObjectType> *)otherDictionary;
[mutableDic setDictionary:dic];
3. 遍历方法
3.1 for 循环遍历
NSDictionary *dic = @{@"key-1":@"value-1", @"key-2":@"value-2"};
NSArray *keyArray = dic.allKeys;
for (int i = 0; i < keyArray.count; i++) {
NSString *key = keyArray[i];
NSString *value = dic[key];
}
3.2 for in 快速枚举
for (NSString *key in dic) {
NSString *value = dic[key];
}
3.3 NSEnumerator 枚举器遍历
NSEnumerator *keyEnumerator = [dic keyEnumerator];
NSString *key;
while (key = [keyEnumerator nextObject]) {
NSString *value = dic[key];
}
//因为字典是无序的,所以也没有正序和逆序的区别
3.4 block遍历
[dic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSLog(@"%@:%@", key, obj);
}];
//多线程并发遍历
[dic enumerateKeysAndObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSLog(@"%@:%@-%@", key, obj, [NSThread currentThread]);
}];