博客> NSDictionary&NSMutableDictionary常用操作梳理
NSDictionary&NSMutableDictionary常用操作梳理
2019-09-18 15:44 评论:0 阅读:1130 诸葛亮倒骑小毛驴
ios NSDictionary&NSMutableDictionary常用操作梳理

NSDictionary&NSMutableDictionary常用操作梳理 同数组(NSArray),字典类NSDictionary也支持Object Literals,允许我们方便地基于字面量定义初始化字典对象。以下基于字面量语法快捷初始化字典(NSDictionary):

1

NSDictionary* literalDictionary = @{@"k1":@"v1", @"k2":@"v2", @"k3":@"v3"};

需要注意的是数组是有序的,按照addObject的顺序存放;字典是无序的,

1.创建初始化(Initialization&Creation)

1.1 Initializing an Dictionary(NS_DESIGNATED_INITIALIZER)

1 2 3 4 5 6 7 8 9

  • (instancetype)init NS_DESIGNATED_INITIALIZER;

// 基于va_list初始化NSDictionary

  • (instancetype)initWithObjectsAndKeys:(id)firstObject, ... NS_REQUIRES_NIL_TERMINATION; // 基于keys数组和对应的values数组初始化NSDictionary
  • (instancetype)initWithObjects:(NSArray )objects forKeys:(NSArray )keys; // 基于other Dictionary初始化新的NSDictionary
  • (instancetype)initWithDictionary:(NSDictionary *)otherDictionary;
  • (instancetype)initWithDictionary:(NSDictionary *)otherDictionary copyItems:(BOOL)flag;

1.2 Creating an Dictionary (autorelease)

1 2 3 4 5 6 7 8 9

  • (instancetype)dictionary; // 以一对key-value初始化NSDictionary
  • (instancetype)dictionaryWithObject:(id)object forKey:(id )key; // initWithObjectsAndKeys:对应的类静态实例化方法
  • (instancetype)dictionaryWithObjectsAndKeys:(id)firstObject, ... NS_REQUIRES_NIL_TERMINATION; // initWithObjects:forKeys:对应的类静态实例化方法
  • (instancetype)dictionaryWithObjects:(NSArray )objects forKeys:(NSArray )keys; // initWithDictionary:对应的类静态实例化方法
  • (instancetype)dictionaryWithDictionary:(NSDictionary *)dict;

以下是简单的示例:

1 2 3 4

NSDictionary queryItemDict1 = [NSDictionary dictionaryWithObjectsAndKeys:@"v1", @"k1", @"v2", @"k2", @"v3", @"k3", nil nil]; NSLog(@"queryItemDict1 = %@", queryItemDict1); NSDictionary queryItemDict2 = [NSDictionary dictionaryWithObjects:@[@"v1", @"v2", @"v3"] forKeys:@[@"k1", @"k2", @"k3"]]; NSLog(@"queryItemDict2 = %@", queryItemDict2);

2.访问字典(Querying)

2.1 字典键值对个数

1

@property (readonly) NSUInteger count;

可以基于dictionary.count对字典进行判空:如果dictionary.count=0,则表示字典为nil或不包含任何键值对。

1 2

@property (readonly, copy) NSArray allKeys; // 所有key的数组 @property (readonly, copy) NSArray allValues; // 所有value的数组

2.2 键值索引查询

1 2 3 4 5 6 7 8

// 查找key对应的value(NSObject)

  • (id)objectForKey:(id)aKey; // 等效于objectForKey,支持中括号下标格式(dictionary[key])访问指定键的值。
  • (id)objectForKeyedSubscript:(id)key NS_AVAILABLE(10_8, 6_0); // 查找value相同的所有keys
  • (NSArray *)allKeysForObject:(id)anObject; // 基于keys数组查找对应的values数组
  • (NSArray )objectsForKeys:(NSArray )keys notFoundMarker:(id)marker;

以下objectsForKeys:notFoundMarker:示例:

1 2 3 4 5 6

NSArray keyArr = @[@"k1", @"k2", @"k3"]; NSArray objArr = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil nil]; NSArray srcArr = [NSArray arrayWithObjects:@"k0", @"k2", nil nil]; NSDictionary dict = [NSDictionary dictionaryWithObjects:objArr forKeys:keyArr]; // NSArray resArr = [dict objectsForKeys:srcArr notFoundMarker:@"not found"]; // ("not found", v2) NSArray resArr = [dict objectsForKeys:srcArr notFoundMarker:[NSNull null]]; // ("", v2)

3.遍历字典(Enumerate)

1 2

  • (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id key, id obj, BOOLBOOL *stop))block NS_AVAILABLE(10_6, 4_0);
  • (NSSet )keysOfEntriesPassingTest:(BOOL (^)(id key, id obj, BOOLBOOL stop))predicate NS_AVAILABLE(10_6, 4_0);

4.字典排序(Sorting)

keysSortedByValueUsingSelector/keysSortedByValueUsingComparator通过使用指定SEL或NSComarator来对allKeys进行排序。

1 2

  • (NSArray *)keysSortedByValueUsingSelector:(SEL)comparator;
  • (NSArray *)keysSortedByValueUsingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0);

5.可变字典(NSMutableDictionary)

5.1 Initializing an Dictionary(NS_DESIGNATED_INITIALIZER)

除了继承NSDictionary基本的init,还增加了以下指定初始化函数:

1

  • (instancetype)initWithCapacity:(NSUInteger)numItems NS_DESIGNATED_INITIALIZER;

例如,以下代码片段获取URL Components查询串中的queryItem Dictionary:

1 2 3 4 5 6 7 8 9

// http://weixin.sogou.com/weixin?type=2&ie=utf-8&query=NASA发现新地球 NSString wxNASAURLPath = @"http://weixin.sogou.com/weixin?type=2&ie=utf-8&query=NASA发现新地球"; NSURLComponents wxNASAURLComponents = [NSURLComponents componentsWithString:wxNASAURLPath]; NSMutableDictionary queryItemDict = [NSMutableDictionary dictionary]; NSArray queryItems = wxNASAURLComponents.queryItems; for (NSURLQueryItem* item in queryItems) { [queryItemDict setObject:item.value forKey:item.name]; } NSLog(@"queryItemDict = %@", queryItemDict);

5.2 setObject for key

1 2 3 4

// 赋值替换键值对

  • (void)setObject:(id)anObject forKey:(id )aKey; // 等效于setObject:forKey:,支持中括号下标格式(dictionary[key]=)赋值替换。
  • (void)setObject:(id)obj forKeyedSubscript:(id )key NS_AVAILABLE(10_8, 6_0);

5.3 addEntries & setDictionary

1 2 3 4

// 从otherDictionary追加entries到当前Dictionary

  • (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary; // 等效于先removeAllObjects后addObjectsFromArray;setDictionary类似于对retain propery的赋值(setter)。
  • (void)setDictionary:(NSDictionary *)otherDictionary;

5.4 removeObject

1 2 3 4 5 6

// 删除指定key的键值对

  • (void)removeObjectForKey:(id)aKey; // 删除指定key数组对应的键值对
  • (void)removeObjectsForKeys:(NSArray *)keyArray; // 删除清空所有键值对
  • (void)removeAllObjects;

6.参考

  • 《NSDictionary API学习》

  • 《OC字典NSDictionary》

  • 《Objective-C研究院之词典对象(八)》

  • 《NSDictionary/NSMutableDictionary 及 NSArray/NSmutableArray (实例)》
收藏
1
sina weixin mail 回到顶部