ios开发之NSDictionary

来源:互联网 发布:程序员 博客 平台 编辑:程序博客网 时间:2024/05/17 17:17

NSMutableDictionary

NSMutableDictionary用于可变长字典。

初始化

- (instancetype)initWithCapacity:(NSUInteger)numItems

初始化字典,设置字典的初始容量。参数numItems为容量大小。

- (instancetype)init

初始化字典。相当于调用 [self initWithCapacity: 0];

- (nullable NSMutableDictionary<KeyType, ObjectType> *)initWithContentsOfFile:(NSString *)path;

根据文件内容初始化字典。

- (nullable NSMutableDictionary<KeyType, ObjectType> *)initWithContentsOfURL:(NSURL *)url;

根据url内容初始化字典。

设置键/值

- (void)setObject:(ObjectType)anObject forKey:(KeyType <NSCopying>)aKey;

设置键/值。参数aKey为键,参数anObject为值。如果不存在键,则添加键/值,如果存在,则重新设置值。设置的键是aKey调用copy的结果。设置的值的引用计数加一。

- (void)setObject:(nullable ObjectType)obj forKeyedSubscript:(KeyType <NSCopying>)key

等同于- (void)setObject: forKey;

删除

- (void)removeObjectForKey:(KeyType)aKey;

根据键删除键/值。

- (void)removeAllObjects;

删除所有。

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

删除数组中的对象。

添加

- (void)addEntriesFromDictionary:(NSDictionary<KeyType, ObjectType> *)otherDictionary;

添加字典。

设置

- (void)setDictionary:(NSDictionary<KeyType, ObjectType> *)otherDictionary;

设置字典。

构造

 + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems;

根据初始化容量构造字典。

+ (nullable NSMutableDictionary<KeyType, ObjectType> *)dictionaryWithContentsOfFile:(NSString *)path;

根据文件内容构造字典

+ (nullable NSMutableDictionary<KeyType, ObjectType> *)dictionaryWithContentsOfURL:(NSURL *)url;

根据url内容构造字典

NSDictionary

初始化

- (instancetype)init
- (instancetype)initWithObjects:(const ObjectType _Nonnull [_Nullable])objects forKeys:(const KeyType <NSCopying> _Nonnull [_Nullable])keys count:(NSUInteger)cnt

参数objects 为值的数组,参数keys为键的数组,参数cnt为数组的个数。
keys的所有成员都调用copy,copy的对象添加进字典。

- (instancetype)initWithObjectsAndKeys:(id)firstObject, ...
- (instancetype)initWithDictionary:(NSDictionary<KeyType, ObjectType> *)otherDictionary;

使用字典初始化字典

- (instancetype)initWithDictionary:(NSDictionary<KeyType, ObjectType> *)otherDictionary copyItems:(BOOL)flag;

使用字典初始化字典,如果参数flag为true,则字典的值都是copy得到的。

- (instancetype)initWithObjects:(NSArray<ObjectType> *)objects forKeys:(NSArray<KeyType <NSCopying>> *)keys;
- (nullable NSDictionary<KeyType, ObjectType> *)initWithContentsOfFile:(NSString *)path;

根据文件内容初始化。

- (nullable NSDictionary<KeyType, ObjectType> *)initWithContentsOfURL:(NSURL *)url;

根据url内容初始化。

构造

 + (instancetype)dictionary;
+ (instancetype)dictionaryWithObject:(ObjectType)object forKey:(KeyType <NSCopying>)key;
+ (instancetype)dictionaryWithObjects:(const ObjectType [])objects forKeys:(const KeyType <NSCopying> [])keys count:(NSUInteger)cnt;
+ (instancetype)dictionaryWithObjectsAndKeys:(id)firstObject, ...
 + (instancetype)dictionaryWithDictionary:(NSDictionary<KeyType, ObjectType> *)dict;
+ (instancetype)dictionaryWithObjects:(NSArray<ObjectType> *)objects forKeys:(NSArray<KeyType <NSCopying>> *)keys;
+ (nullable NSDictionary<KeyType, ObjectType> *)dictionaryWithContentsOfFile:(NSString *)path;
+ (nullable NSDictionary<KeyType, ObjectType> *)dictionaryWithContentsOfURL:(NSURL *)url;

返回个数

@property (readonly) NSUInteger count;

返回值

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

根据参数aKey返回值,如果没有对应的值则返回nil;

- (nullable ObjectType)objectForKeyedSubscript:(KeyType)key

返回所有键

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

所有键都调用copy。

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

返回值对应的所有键。

返回所有值

@property (readonly, copy) NSArray<ObjectType> *allValues;
- (NSArray<ObjectType> *)objectsForKeys:(NSArray<KeyType> *)keys notFoundMarker:(ObjectType)marker;

返回描述

@property (readonly, copy) NSString *description;@property (readonly, copy) NSString *descriptionInStringsFileFormat;- (NSString *)descriptionWithLocale:(nullable id)locale;- (NSString *)descriptionWithLocale:(nullable id)locale indent:(NSUInteger)level;

是否相等

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

排序

- (NSArray<KeyType> *)keysSortedByValueUsingSelector:(SEL)comparator;- (NSArray<KeyType> *)keysSortedByValueUsingComparator:(NSComparator NS_NOESCAPE)cmptr- (NSArray<KeyType> *)keysSortedByValueWithOptions:(NSSortOptions)opts usingComparator:(NSComparator NS_NOESCAPE)cmptr

枚举器

- (NSEnumerator<KeyType> *)keyEnumerator;

键的枚举器

- (NSEnumerator<ObjectType> *)objectEnumerator;

值的枚举器

写入文件/url

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically; 
0 0
原创粉丝点击