美文网首页
OC集合类之NSDictionary、NSMutableDict

OC集合类之NSDictionary、NSMutableDict

作者: 浅_若清风 | 来源:发表于2018-05-07 16:52 被阅读0次

    一、NSDictionary(不可变字典)

    概述:

    在编程中,字典(dictionary)是关键字及其定义的集合。NSDictionary是Cocoa中的一个集合类,它能在给定的关键字(通常是一个NSString字符串)下存储一个数值(可以是任何类型的OC对象),然后根据这个关键字来查找相应的数据。

    实例方法:

    
    //根据key查找akey对应的值
    
    - (nullableObjectType)objectForKey:(KeyType)aKey;
    
    //获取字典所有键值
    
    - (NSEnumerator *)keyEnumerator;
    
    //初始化字典
    
    - (instancetype)init NS_DESIGNATED_INITIALIZER;
    
    //初始化字典(根据不同的编译条件)
    
    #if TARGET_OS_WIN32
    
    - (instancetype)initWithObjects:(constObjectType_Nonnull[_Nullable])objects forKeys:(constKeyType_Nonnull[_Nullable])keys count:(NSUInteger)cnt;
    
    #else
    
    - (instancetype)initWithObjects:(constObjectType_Nonnull[_Nullable])objects forKeys:(constKeyType_Nonnull[_Nullable])keys count:(NSUInteger)cntNS_DESIGNATED_INITIALIZER;
    
    #endif
    
    - (nullableinstancetype)initWithCoder:(NSCoder*)aDecoderNS_DESIGNATED_INITIALIZER;
    
    //根据所填入的object返回对应所有的key键值
    
    - (NSArray *)allKeysForObject:(ObjectType)anObject;
    
    //根据设置的locale进行连接数组
    
    - (NSString*)descriptionWithLocale:(nullableid)locale;
    
    //根据设置的locale进行连接数组
    
    - (NSString*)descriptionWithLocale:(nullableid)locale indent:(NSUInteger)level;
    
    //字典判断
    
    - (BOOL)isEqualToDictionary:(NSDictionary *)otherDictionary;
    
    //获取字典所有对象值
    
    - (NSEnumerator *)objectEnumerator;
    
    //字典将某个特定的数组作为key值传进去得到对应的value,如果某个key找不到对应的key,就用notFoundMarker提前设定的值代替
    
    - (NSArray *)objectsForKeys:(NSArray *)keys notFoundMarker:(ObjectType)marker;
    
    //将字典序列化为NSRabryType格式中指定的URL
    
    - (BOOL)writeToURL:(NSURL*)url error:(NSError**)errorAPI_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0));
    
    //排序
    
    - (NSArray *)keysSortedByValueUsingSelector:(SEL)comparator;
    
    - (void)getObjects:(ObjectType_Nonnull__unsafe_unretained[_Nullable])objects andKeys:(KeyType_Nonnull__unsafe_unretained[_Nullable])keys count:(NSUInteger)countAPI_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0))NS_SWIFT_UNAVAILABLE("Use 'allKeys' and/or 'allValues' instead");
    
    //以数组下标的形式来访问相应键的值
    
    - (nullableObjectType)objectForKeyedSubscript:(KeyType)keyAPI_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0));
    
    //利用block对字典进行遍历
    
    - (void)enumerateKeysAndObjectsUsingBlock:(void(NS_NOESCAPE^)(KeyType key, ObjectType obj,BOOL*stop))blockAPI_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
    
    //利用block对字典进行遍历(添加正序反序遍历)
    
    - (void)enumerateKeysAndObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void(NS_NOESCAPE^)(KeyType key, ObjectType obj,BOOL*stop))blockAPI_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
    
    //根据NSComparato参数条件排序
    
    - (NSArray *)keysSortedByValueUsingComparator:(NSComparatorNS_NOESCAPE)cmptrAPI_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
    
    //根据NSComparato参数条件排序
    
    - (NSArray *)keysSortedByValueWithOptions:(NSSortOptions)opts usingComparator:(NSComparatorNS_NOESCAPE)cmptrAPI_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
    
    //过滤的方法
    
    - (NSSet *)keysOfEntriesPassingTest:(BOOL(NS_NOESCAPE^)(KeyType key, ObjectType obj,BOOL*stop))predicateAPI_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
    
    //过滤的方法
    
    - (NSSet *)keysOfEntriesWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL(NS_NOESCAPE^)(KeyType key, ObjectType obj,BOOL*stop))predicateAPI_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
    
    //根据文件创建字典
    
    - (nullableNSDictionary *)initWithContentsOfFile:(NSString*)path;
    
    //根据url创建字典
    
    - (nullableNSDictionary *)initWithContentsOfURL:(NSURL*)url;
    
    //将字典写进特定的路径path
    
    - (BOOL)writeToFile:(NSString*)path atomically:(BOOL)useAuxiliaryFile;
    
    //将字典写进特定的url
    
    - (BOOL)writeToURL:(NSURL*)url atomically:(BOOL)atomically; 
    
    //使用指定的以nil为结尾的对象与键对列表初始化列表
    
    - (instancetype)initWithObjectsAndKeys:(id)firstObject, ...NS_REQUIRES_NIL_TERMINATION;
    
    //使用另一个字典初始化字典
    
    - (instancetype)initWithDictionary:(NSDictionary *)otherDictionary;
    
    //使用另一个字典初始化字典,还可以为每个对象创建新的副本
    
    - (instancetype)initWithDictionary:(NSDictionary *)otherDictionary copyItems:(BOOL)flag;
    
    //初始化
    
    - (instancetype)initWithObjects:(NSArray *)objects forKeys:(NSArray> *)keys;
    
    //从指定的URL读取存储在NSpRealType格式中的字典
    
    - (nullableNSDictionary *)initWithContentsOfURL:(NSURL*)url error:(NSError**)errorAPI_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0));
    
    

    类方法:

    
    //根据path路径获取字典信息
    
    + (nullableNSDictionary *)dictionaryWithContentsOfFile:(NSString*)path;
    
    //根据url地址获取字典信息
    
    + (nullableNSDictionary *)dictionaryWithContentsOfURL:(NSURL*)url;
    
    //创建字典并赋值
    
    + (instancetype)dictionaryWithObject:(ObjectType)object forKey:(KeyType)key;
    
    //创建字典(根据不同编译环境)
    
    #if TARGET_OS_WIN32
    
    + (instancetype)dictionaryWithObjects:(constObjectType_Nonnull[_Nullable])objects forKeys:(constKeyType_Nonnull[_Nullable])keys count:(NSUInteger)cnt;
    
    #else
    
    + (instancetype)dictionaryWithObjects:(constObjectType_Nonnull[_Nullable])objects forKeys:(constKeyType_Nonnull[_Nullable])keys count:(NSUInteger)cnt;
    
    #endif
    
    + (instancetype)dictionaryWithObjectsAndKeys:(id)firstObject, ...NS_REQUIRES_NIL_TERMINATIONNS_SWIFT_UNAVAILABLE("Use dictionary literals instead");
    
    //创建字典并赋值一个字典
    
    + (instancetype)dictionaryWithDictionary:(NSDictionary *)dict;
    
    //创建字典并通过数组赋值
    
    + (instancetype)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray> *)keys;
    
    //从指定的URL读取存储在NSpRealType格式中的字典
    
    + (nullableNSDictionary *)dictionaryWithContentsOfURL:(NSURL*)url error:(NSError**)errorAPI_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0))NS_SWIFT_UNAVAILABLE("Use initializer instead");
    
    //用来创建预订好的key集合,返回的值作为NSMutableDictionary的dictionaryWithSharesKeySet参数传入,可以重用key,从而节约copy操作,节省内存
    
    + (id)sharedKeySetForKeys:(NSArray> *)keysAPI_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0));
    
    //创建字典,共享键集会复用对象
    
    + (NSMutableDictionary *)dictionaryWithSharedKeySet:(id)keysetAPI_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0));
    
    

    二、NSMutableDictionary(可变字典)

    概述:

    和NSString和NSArray一样,NSDictionary对象是不可变。NSMutableDictionary继承至NSDictionary,允许我们随意添加、删除元素。

    实例方法:

    
    //删除字典对应key值的元素
    
    - (void)removeObjectForKey:(KeyType)aKey;
    
    //添加元素(anObject为要添加的元素,aKey为对应的key值)
    
    - (void)setObject:(ObjectType)anObject forKey:(KeyType)aKey;
    
    //初始化
    
    - (instancetype)init NS_DESIGNATED_INITIALIZER;
    
    //指定初始化函数
    
    - (instancetype)initWithCapacity:(NSUInteger)numItemsNS_DESIGNATED_INITIALIZER;
    
    - (nullableinstancetype)initWithCoder:(NSCoder*)aDecoderNS_DESIGNATED_INITIALIZER;
    
    //字典拼接
    
    - (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary;
    
    //删除字典
    
    - (void)removeAllObjects;
    
    //移除由keyArray的元素指定的key的所有数据
    
    - (void)removeObjectsForKeys:(NSArray *)keyArray;
    
    //设置字典内容为otherDictionary
    
    - (void)setDictionary:(NSDictionary *)otherDictionary;
    
    //前缀下标方法
    
    - (void)setObject:(nullableObjectType)obj forKeyedSubscript:(KeyType)keyAPI_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0));
    
    //根据文件创建字典
    
    - (nullableNSMutableDictionary *)initWithContentsOfFile:(NSString*)path;
    
    //根据url创建字典
    
    - (nullableNSMutableDictionary *)initWithContentsOfURL:(NSURL*)url;
    

    类方法:

    
    //初始化一个空间大小的字典(NSUInteger代表了开辟内存的一个单位)
    
    + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems;
    
    //根据文件获取字典信息
    
    + (nullableNSMutableDictionary *)dictionaryWithContentsOfFile:(NSString*)path;
    
    //根据url地址获取字典信息
    
    + (nullableNSMutableDictionary *)dictionaryWithContentsOfURL:(NSURL*)url;
    
    

    说明:

    字典(也被称为散列表或关联数组)使用的是键查询的优化方式。它可以立即找出要查询的数据,而不需要遍历整个数组。对应大型的数据集来说,使用字典比数组要快得多,所以,这就是我们不用数组来存储然后查询的原因。

    相关文章

      网友评论

          本文标题:OC集合类之NSDictionary、NSMutableDict

          本文链接:https://www.haomeiwen.com/subject/qjjvrftx.html