NSDictionaryNSMutableDictionary常用操作梳理

1.创建初始化(Initialization&Creation)1.1 Initializing an Dictionary(NS_DESIGNATED_INITIALIZER)- (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)

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

以下是简单的示例:

NSDictionary* queryItemDict1 = [NSDictionary dictionaryWithObjectsAndKeys:@"v1", @"k1", @"v2", @"k2", @"v3", @"k3", nil];NSLog(@"queryItemDict1 = %@", queryItemDict1);NSDictionary* queryItemDict2 = [NSDictionary dictionaryWithObjects:@[@"v1", @"v2", @"v3"] forKeys:@[@"k1", @"k2", @"k3"]];NSLog(@"queryItemDict2 = %@", queryItemDict2);2.访问字典(Querying)2.1字典键值对个数@property (readonly) NSUInteger count;

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

@property (readonly, copy) NSArray *allKeys; // 所有key的数组@property (readonly, copy) NSArray *allValues; // 所有value的数组// 查找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;

3.遍历字典(Enumerate)

– (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id key, id obj, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);

– (NSSet *)keysOfEntriesPassingTest:(BOOL (^)(id key, id obj, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);

4.字典排序(Sorting)

– (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,,还增加了以下指定初始化函数:

– (instancetype)initWithCapacity:(NSUInteger)numItems NS_DESIGNATED_INITIALIZER;例如,以下代码片段获取URL Components查询串中的queryItem Dictionary: // ?type=2&ie=utf-8&query=NASA发现新地球NSString* wxNASAURLPath = @"?type=2&ie=utf-8&query=NASA%E5%8F%91%E7%8E%B0%E6%96%B0%E5%9C%B0%E7%90%83";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// 赋值替换键值对- (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey;// 等效于setObject:forKey:,支持中括号下标格式(dictionary[key]=)赋值替换。- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key NS_AVAILABLE(10_8, 6_0);

5.3 addEntries & setDictionary// 从otherDictionary追加entries到当前Dictionary- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary;// 等效于先removeAllObjects后addObjectsFromArray;setDictionary类似于对retain propery的赋值(setter)。- (void)setDictionary:(NSDictionary *)otherDictionary;

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

教育人的诗句或名言警句,激励人在逆境中脱颖而出的话

NSDictionaryNSMutableDictionary常用操作梳理

相关文章:

你感兴趣的文章:

标签云: