NSMutableDictionary简单细说(一)—— 整体了解

版本记录

版本号 时间
V1.0 2017.08.28

前言

NSMutableDictionary是可变字典,相对NSDictionary来说,它是可变的,它的可变性可以参考NSMutableArray数组,但是它与数组还是有很大不同,尽管他们都属于集合类,下面这几篇我们继续来将一下基础类的知识。还是老规矩从整体到局部,从浅入深进行讲解,谢谢大家。

基本了解

NSMutableDictionary是可变字典,它的一个重要的作用就是可以自由的更改里面键值对的数目,这一点很像NSMutableArray


API

下面我们就看一下,苹果给我们提供的API文档。

/****************   Mutable Dictionary  ****************/

@interface NSMutableDictionary : NSDictionary

- (void)removeObjectForKey:(KeyType)aKey;
- (void)setObject:(ObjectType)anObject forKey:(KeyType )aKey;
- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithCapacity:(NSUInteger)numItems NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;

@end

@interface NSMutableDictionary (NSExtendedMutableDictionary)

- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary;
- (void)removeAllObjects;
- (void)removeObjectsForKeys:(NSArray *)keyArray;
- (void)setDictionary:(NSDictionary *)otherDictionary;
- (void)setObject:(nullable ObjectType)obj forKeyedSubscript:(KeyType )key NS_AVAILABLE(10_8, 6_0);

@end

@interface NSMutableDictionary (NSMutableDictionaryCreation)

+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems;

+ (nullable NSMutableDictionary *)dictionaryWithContentsOfFile:(NSString *)path;
+ (nullable NSMutableDictionary *)dictionaryWithContentsOfURL:(NSURL *)url;
- (nullable NSMutableDictionary *)initWithContentsOfFile:(NSString *)path;
- (nullable NSMutableDictionary *)initWithContentsOfURL:(NSURL *)url;

@end

@interface NSMutableDictionary (NSSharedKeySetDictionary)

/*  Create a mutable dictionary which is optimized for dealing with a known set of keys.
 Keys that are not in the key set can still be set into the dictionary, but that usage is not optimal.
 As with any dictionary, the keys must be copyable.
 If keyset is nil, an exception is thrown.
 If keyset is not an object returned by +sharedKeySetForKeys:, an exception is thrown.
 */
+ (NSMutableDictionary *)dictionaryWithSharedKeySet:(id)keyset NS_AVAILABLE(10_8, 6_0);

@end

从上面的API中我们可以看出来,NSMutableDictionary本类一个,分类有NSExtendedMutableDictionaryNSMutableDictionaryCreationNSSharedKeySetDictionary三个。


文档结构

下面我们看一下苹果给我们的开发文档结构。

NSMutableDictionary简单细说(一)—— 整体了解_第1张图片
NSMutableDictionary简单细说(一)—— 整体了解_第2张图片

后记

未完,待续~~~

NSMutableDictionary简单细说(一)—— 整体了解_第3张图片

你可能感兴趣的:(NSMutableDictionary简单细说(一)—— 整体了解)