NSDictionary和NSMutableDictionary整理与总结

一、NSDictionary

1.NSDictionary的初始化

(1)NSDictionary内存初始化

NSDictionary *dictionary = @{key:value,...};
- (instancetype)initWithObjectsAndKeys:(id)firstObject, ... NS_REQUIRES_NIL_TERMINATION;
- (instancetype)initWithDictionary:(NSDictionary<KeyType, ObjectType> *)otherDictionary;
- (instancetype)initWithDictionary:(NSDictionary<KeyType, ObjectType> *)otherDictionary copyItems:(BOOL)flag;
- (instancetype)initWithObjects:(NSArray<ObjectType> *)objects forKeys:(NSArray<KeyType <NSCopying>> *)keys;

+ (instancetype)dictionary;
+ (instancetype)dictionaryWithObject:(ObjectType)object forKey:(KeyType <NSCopying>)key;
#if TARGET_OS_WIN32
+ (instancetype)dictionaryWithObjects:(const ObjectType [])objects forKeys:(const KeyType [])keys count:(NSUInteger)cnt;
#else
+ (instancetype)dictionaryWithObjects:(const ObjectType [])objects forKeys:(const KeyType <NSCopying> [])keys count:(NSUInteger)cnt;
#endif

+ (instancetype)dictionaryWithObjectsAndKeys:(id)firstObject, ... NS_REQUIRES_NIL_TERMINATION NS_SWIFT_UNAVAILABLE("Use dictionary literals instead");

+ (instancetype)dictionaryWithDictionary:(NSDictionary<KeyType, ObjectType> *)dict;
+ (instancetype)dictionaryWithObjects:(NSArray<ObjectType> *)objects forKeys:(NSArray<KeyType <NSCopying>> *)keys;

(2)NSDictionary从文件或链接初始化

+ (nullable NSDictionary<KeyType, ObjectType> *)dictionaryWithContentsOfFile:(NSString *)path;
+ (nullable NSDictionary<KeyType, ObjectType> *)dictionaryWithContentsOfURL:(NSURL *)url;
- (nullable NSDictionary<KeyType, ObjectType> *)initWithContentsOfFile:(NSString *)path;
- (nullable NSDictionary<KeyType, ObjectType> *)initWithContentsOfURL:(NSURL *)url;

2.将NSDictionary写入文件或链接

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;

- // the atomically flag is ignored if url of a type that cannot be written atomically.
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically; 

3.NSDictionary获取键值对个数

@property (readonly) NSUInteger count;

4.NSDictionary获取key或value

(1)获取所有key

@property (readonly, copy) NSArray<KeyType> *allKeys;

(2)获取所有value

@property (readonly, copy) NSArray<ObjectType> *allValues;

(3)通过key获取value

- (nullable ObjectType)objectForKey:(KeyType)aKey;

(4)获取value对应的所有key

- (NSArray<KeyType> *)allKeysForObject:(ObjectType)anObject;

5.NSDictioary判断字典是否相等

- (BOOL)isEqualToDictionary:(NSDictionary<KeyType, ObjectType> *)otherDictionary;

6.NSDictionary排序

- (NSArray<KeyType> *)keysSortedByValueUsingSelector:(SEL)comparator; - (NSArray<KeyType> *)keysSortedByValueUsingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0); - (NSArray<KeyType> *)keysSortedByValueWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0);

7.NSDictionary操作器

- (void)enumerateKeysAndObjectsUsingBlock:(void (^)(KeyType key, ObjectType obj, BOOL *stop))block NS_AVAILABLE(10_6, 4_0); - (void)enumerateKeysAndObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(KeyType key, ObjectType obj, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);

二、NSMutableDictionary

1.NSMutableDictionary初始化

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

+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems;

2.NSMutableDictionary操作

(1)增加键值对

//当key存在时修改value,不存在时添加键值对
- (void)addEntriesFromDictionary:(NSDictionary<KeyType, ObjectType> *)otherDictionary;

(2)删除键值对

- (void)removeAllObjects; - (void)removeObjectForKey:(KeyType)aKey; - (void)removeObjectsForKeys:(NSArray<KeyType> *)keyArray;

(3)重置键值对

//当key存在时修改value,不存在时添加键值对
- (void)setObject:(ObjectType)anObject forKey:(KeyType <NSCopying>)aKey; - (void)setObject:(nullable ObjectType)obj forKeyedSubscript:(KeyType <NSCopying>)key NS_AVAILABLE(10_8, 6_0); - (void)setDictionary:(NSDictionary<KeyType, ObjectType> *)otherDictionary;

你可能感兴趣的:(Dictionary)