NSDictionary&NSMutableDictionary常用操作

转载 :http://blog.csdn.net/phunxm/article/details/47072831




同数组(NSArray),字典类NSDictionary也支持字面量语法糖Object Literals Syntactic Sugar,允许我们方便地基于字面量定义初始化字典对象。以下基于字面量语法快捷初始化字典(NSDictionary):

[objc]  view plain  copy
 print ?
  1. NSDictionary* literalDictionary = @{@"k1":@"v1"@"k2":@"v2"@"k3":@"v3"};  

需要注意的是数组是有序的,按照addObject的顺序存放;字典是无序的,在进行枚举时,读取的键-值顺序不一定和写入的顺序一致。

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

1.1 Initializing an Dictionary(NS_DESIGNATED_INITIALIZER)

[objc]  view plain  copy
 print ?
  1. - (instancetype)init NS_DESIGNATED_INITIALIZER;  
  2.   
  3. // 基于va_list初始化NSDictionary  
  4. - (instancetype)initWithObjectsAndKeys:(id)firstObject, ... NS_REQUIRES_NIL_TERMINATION;  
  5. // 基于keys数组和对应的values数组初始化NSDictionary  
  6. - (instancetype)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;  
  7. // 基于other Dictionary初始化新的NSDictionary  
  8. - (instancetype)initWithDictionary:(NSDictionary *)otherDictionary;  
  9. - (instancetype)initWithDictionary:(NSDictionary *)otherDictionary copyItems:(BOOL)flag;  

1.2 Creating an Dictionary (autorelease)

[objc]  view plain  copy
 print ?
  1. + (instancetype)dictionary;  
  2. // 以一对key-value初始化NSDictionary  
  3. + (instancetype)dictionaryWithObject:(id)object forKey:(id <NSCopying>)key;  
  4. // initWithObjectsAndKeys:对应的类静态实例化方法  
  5. + (instancetype)dictionaryWithObjectsAndKeys:(id)firstObject, ... NS_REQUIRES_NIL_TERMINATION;  
  6. // initWithObjects:forKeys:对应的类静态实例化方法  
  7. + (instancetype)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;  
  8. // initWithDictionary:对应的类静态实例化方法  
  9. + (instancetype)dictionaryWithDictionary:(NSDictionary *)dict;  

以下是简单的示例:

[objc]  view plain  copy
 print ?
  1. NSDictionary* queryItemDict1 = [NSDictionary dictionaryWithObjectsAndKeys:@"v1"@"k1"@"v2"@"k2"@"v3"@"k3", nil nil];  
  2. NSLog(@"queryItemDict1 = %@", queryItemDict1);  
  3. NSDictionary* queryItemDict2 = [NSDictionary  dictionaryWithObjects:@[@"v1"@"v2"@"v3"] forKeys:@[@"k1"@"k2"@"k3"]];  
  4. NSLog(@"queryItemDict2 = %@", queryItemDict2);  

2.访问字典(Querying)

2.1 字典键值对个数

[objc]  view plain  copy
 print ?
  1. @property (readonly) NSUInteger count;  

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

[objc]  view plain  copy
 print ?
  1. @property (readonlycopyNSArray *allKeys; // 所有key的数组  
  2. @property (readonlycopyNSArray *allValues; // 所有value的数组  

2.2 键值索引查询

[objc]  view plain  copy
 print ?
  1. // 查找key对应的value(NSObject)  
  2. - (id)objectForKey:(id)aKey;  
  3. // 等效于objectForKey,支持中括号下标格式(dictionary[key])访问指定键的值。  
  4. - (id)objectForKeyedSubscript:(id)key NS_AVAILABLE(10_86_0);  
  5. // 查找value相同的所有keys  
  6. - (NSArray *)allKeysForObject:(id)anObject;  
  7. // 基于keys数组查找对应的values数组  
  8. - (NSArray *)objectsForKeys:(NSArray *)keys notFoundMarker:(id)marker;  

以下objectsForKeys:notFoundMarker:示例:

[objc]  view plain  copy
 print ?
  1. NSArray *keyArr = @[@"k1"@"k2"@"k3"];  
  2. NSArray *objArr = [NSArray arrayWithObjects:@"v1"@"v2"@"v3", nil nil];  
  3. NSArray *srcArr = [NSArray arrayWithObjects:@"k0"@"k2", nil nil];  
  4. NSDictionary *dict = [NSDictionary dictionaryWithObjects:objArr forKeys:keyArr];  
  5. // NSArray *resArr = [dict objectsForKeys:srcArr notFoundMarker:@"not found"]; // ("not found", v2)  
  6. NSArray *resArr = [dict objectsForKeys:srcArr notFoundMarker:[NSNull null]]; // ("<null>", v2)  

3.遍历字典(Enumerate)

[objc]  view plain  copy
 print ?
  1. - (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id key, id obj, BOOLBOOL *stop))block NS_AVAILABLE(10_64_0);  
  2. - (NSSet *)keysOfEntriesPassingTest:(BOOL (^)(id key, id obj, BOOLBOOL *stop))predicate NS_AVAILABLE(10_64_0);  

4.字典排序(Sorting)

keysSortedByValueUsingSelector/keysSortedByValueUsingComparator通过使用指定SELNSComarator来对allKeys进行排序,然后通过objectsForKeys取出排序后的键-值对。

[objc]  view plain  copy
 print ?
  1. - (NSArray *)keysSortedByValueUsingSelector:(SEL)comparator;  
  2. - (NSArray *)keysSortedByValueUsingComparator:(NSComparator)cmptr NS_AVAILABLE(10_64_0);  

5.可变字典(NSMutableDictionary)

5.1 Initializing an Dictionary(NS_DESIGNATED_INITIALIZER)

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

[objc]  view plain  copy
 print ?
  1. - (instancetype)initWithCapacity:(NSUInteger)numItems NS_DESIGNATED_INITIALIZER;  

5.2 setObject for key

[objc]  view plain  copy
 print ?
  1. // 赋值替换键值对  
  2. - (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey;  
  3. // 等效于setObject:forKey:,支持中括号下标格式(dictionary[key]=)赋值替换。  
  4. - (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key NS_AVAILABLE(10_86_0);  

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

[objc]  view plain  copy
 print ?
  1. // http://weixin.sogou.com/weixin?type=2&ie=utf-8&query=NASA发现新地球  
  2. NSString* wxNASAURLPath = @"http://weixin.sogou.com/weixin?type=2&ie=utf-8&query=NASA%E5%8F%91%E7%8E%B0%E6%96%B0%E5%9C%B0%E7%90%83";  
  3. NSURLComponents* wxNASAURLComponents =  [NSURLComponents componentsWithString:wxNASAURLPath];  
  4. NSMutableDictionary* queryItemDict = [NSMutableDictionary dictionary];  
  5. NSArray* queryItems = wxNASAURLComponents.queryItems;  
  6. for (NSURLQueryItem* item in queryItems) {  
  7.     [queryItemDict setObject:item.value forKey:item.name];  
  8. }  
  9. NSLog(@"queryItemDict = %@", queryItemDict);  

5.3 addEntries & setDictionary

[objc]  view plain  copy
 print ?
  1. // 从otherDictionary追加entries到当前Dictionary  
  2. - (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary;  
  3. // 等效于先removeAllObjects后addObjectsFromArray;setDictionary类似于对retain propery的赋值(setter)。  
  4. - (void)setDictionary:(NSDictionary *)otherDictionary;  

5.4 removeObject

[objc]  view plain  copy
 print ?
  1. // 删除指定key的键值对  
  2. - (void)removeObjectForKey:(id)aKey;  
  3. // 删除指定key数组对应的键值对  
  4. - (void)removeObjectsForKeys:(NSArray *)keyArray;  
  5. // 删除清空所有键值对  
  6. - (void)removeAllObjects;  


6.参考

NSDictionary API学习OC字典NSDictionary

OC基础集合类Objective-C研究院之词典对象(八)

NSDictionary/NSMutableDictionary 及 NSArray/NSmutableArray (实例)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值