一、NSDictionary(不可變字典)
概述:
在編程中,字典(dictionary)是關鍵字及其定義的集合。NSDictionary是Cocoa中的一個集合類,它能在給定的關鍵字(通常是一個NSString字符串)下存儲一個數(shù)值(可以是任何類型的OC對象),然后根據(jù)這個關鍵字來查找相應的數(shù)據(jù)。
實例方法:
//根據(jù)key查找akey對應的值
- (nullableObjectType)objectForKey:(KeyType)aKey;
//獲取字典所有鍵值
- (NSEnumerator *)keyEnumerator;
//初始化字典
- (instancetype)init NS_DESIGNATED_INITIALIZER;
//初始化字典(根據(jù)不同的編譯條件)
#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;
//根據(jù)所填入的object返回對應所有的key鍵值
- (NSArray *)allKeysForObject:(ObjectType)anObject;
//根據(jù)設置的locale進行連接數(shù)組
- (NSString*)descriptionWithLocale:(nullableid)locale;
//根據(jù)設置的locale進行連接數(shù)組
- (NSString*)descriptionWithLocale:(nullableid)locale indent:(NSUInteger)level;
//字典判斷
- (BOOL)isEqualToDictionary:(NSDictionary *)otherDictionary;
//獲取字典所有對象值
- (NSEnumerator *)objectEnumerator;
//字典將某個特定的數(shù)組作為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");
//以數(shù)組下標的形式來訪問相應鍵的值
- (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));
//根據(jù)NSComparato參數(shù)條件排序
- (NSArray *)keysSortedByValueUsingComparator:(NSComparatorNS_NOESCAPE)cmptrAPI_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
//根據(jù)NSComparato參數(shù)條件排序
- (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));
//根據(jù)文件創(chuàng)建字典
- (nullableNSDictionary *)initWithContentsOfFile:(NSString*)path;
//根據(jù)url創(chuàng)建字典
- (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;
//使用另一個字典初始化字典,還可以為每個對象創(chuàng)建新的副本
- (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));
類方法:
//根據(jù)path路徑獲取字典信息
+ (nullableNSDictionary *)dictionaryWithContentsOfFile:(NSString*)path;
//根據(jù)url地址獲取字典信息
+ (nullableNSDictionary *)dictionaryWithContentsOfURL:(NSURL*)url;
//創(chuàng)建字典并賦值
+ (instancetype)dictionaryWithObject:(ObjectType)object forKey:(KeyType)key;
//創(chuàng)建字典(根據(jù)不同編譯環(huán)境)
#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");
//創(chuàng)建字典并賦值一個字典
+ (instancetype)dictionaryWithDictionary:(NSDictionary *)dict;
//創(chuàng)建字典并通過數(shù)組賦值
+ (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");
//用來創(chuàng)建預訂好的key集合,返回的值作為NSMutableDictionary的dictionaryWithSharesKeySet參數(shù)傳入,可以重用key,從而節(jié)約copy操作,節(jié)省內存
+ (id)sharedKeySetForKeys:(NSArray> *)keysAPI_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0));
//創(chuàng)建字典,共享鍵集會復用對象
+ (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;
//指定初始化函數(shù)
- (instancetype)initWithCapacity:(NSUInteger)numItemsNS_DESIGNATED_INITIALIZER;
- (nullableinstancetype)initWithCoder:(NSCoder*)aDecoderNS_DESIGNATED_INITIALIZER;
//字典拼接
- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary;
//刪除字典
- (void)removeAllObjects;
//移除由keyArray的元素指定的key的所有數(shù)據(jù)
- (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));
//根據(jù)文件創(chuàng)建字典
- (nullableNSMutableDictionary *)initWithContentsOfFile:(NSString*)path;
//根據(jù)url創(chuàng)建字典
- (nullableNSMutableDictionary *)initWithContentsOfURL:(NSURL*)url;
類方法:
//初始化一個空間大小的字典(NSUInteger代表了開辟內存的一個單位)
+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems;
//根據(jù)文件獲取字典信息
+ (nullableNSMutableDictionary *)dictionaryWithContentsOfFile:(NSString*)path;
//根據(jù)url地址獲取字典信息
+ (nullableNSMutableDictionary *)dictionaryWithContentsOfURL:(NSURL*)url;
說明:
字典(也被稱為散列表或關聯(lián)數(shù)組)使用的是鍵查詢的優(yōu)化方式。它可以立即找出要查詢的數(shù)據(jù),而不需要遍歷整個數(shù)組。對應大型的數(shù)據(jù)集來說,使用字典比數(shù)組要快得多,所以,這就是我們不用數(shù)組來存儲然后查詢的原因。