@property (readonly)
NSUInteger count;
//1.利用指定的key尋找對應的value
- (id)objectForKey:(id)aKey;
//2. keyEnumerator得到一個字典的所有鍵值
- (NSEnumerator *)keyEnumerator;
//3.初始化字典
- (instancetype)init
NS_DESIGNATED_INITIALIZER;
//4.條件編譯根據不同情況來初始化字典
#if TARGET_OS_WIN32
- (instancetype)initWithObjects:(const
id [])objects forKeys:(const
id [])keys count:(NSUInteger)cnt;
#else
- (instancetype)initWithObjects:(const
id [])objects forKeys:(const
id <NSCopying> [])keys count:(NSUInteger)cnt
NS_DESIGNATED_INITIALIZER;
#endif
- (instancetype)initWithCoder:(NSCoder *)aDecoder
NS_DESIGNATED_INITIALIZER;
@end
@interface NSDictionary (NSExtendedDictionary)
//5.數組所有key屬性
@property (readonly,
copy) NSArray *allKeys;
//6.根據所填入的object
返回對應所有的key鍵值
- (NSArray *)allKeysForObject:(id)anObject;
//7.屬性 字典所有value
@property (readonly,
copy) NSArray *allValues;
//8.屬性
字符串描述
@property (readonly,
copy) NSString *description;
//9.屬性
字符串描述文件格式
@property (readonly,
copy) NSString *descriptionInStringsFileFormat;
//10.根據設置的locale進行連接數組
- (NSString *)descriptionWithLocale:(id)locale;
//11.根據設置的locale進行連接數組
- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level;
//12.判斷字典是否相等
- (BOOL)isEqualToDictionary:(NSDictionary *)otherDictionary;
//13.得到一個字典的所有values
- (NSEnumerator *)objectEnumerator;
//14.字典將某個特定的數組作為key值傳進去得到對應的value,如果某個key找不到對應的key,就用notFoundMarker提前設定的值代替
- (NSArray *)objectsForKeys:(NSArray *)keys notFoundMarker:(id)marker;
//NSDictionary *dic=[NSDictionary
dictionaryWithObjectsAndKeys:@"K1",@"V1",@"K2",@"V2",@"K3",@"V3",
nil];
//NSArray *arr1=[NSArray
arrayWithObjects:@"V1",@"V2",@"VG",
nil];
//NSArray *ARR= [dic
objectsForKeys:arr1 notFoundMarker:@"BB"];
//NSLog(@"測試測試%@",ARR);
//打印:
//2015-06-08 11:30:54.139 NSDictionary[1624:64989]
測試測試(
//K1,
//BB,
//BB
//)
//15.將字典寫進特定的路徑path
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically;
// the atomically flag is ignored if url of a type that cannot be written atomically.
//16.字典按照value的大小順序來對keys鍵值進行排序(通過value排序,返回key集合)
- (NSArray *)keysSortedByValueUsingSelector:(SEL)comparator;
//NSDictionary *dic1=[NSDictionary
dictionaryWithObjectsAndKeys:@"4",@"A",@"6",@"C",@"5",@"B",
nil];
//NSArray *arr2= [dic1
keysSortedByValueUsingSelector:@selector(compare:)];
//NSLog(@"奇葩奇葩%@",arr2);
//2015-06-08 14:41:59.152 NSDictionary[2749:117502]
奇葩奇葩(
//A,
//B,
//C
//)
//17.
- (void)getObjects:(id
__unsafe_unretained [])objects andKeys:(id
__unsafe_unretained [])keys;
//18.
- (id)objectForKeyedSubscript:(id)key
NS_AVAILABLE(10_8,
6_0);
//19.利用block對字典進行遍歷
- (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id key,
id obj, BOOL *stop))block
NS_AVAILABLE(10_6,
4_0);
//例子:
NSDictionary *dic = [NSDictionary
dictionaryWithObjects:@[@"1",@"2",@"3"]
forKeys:@[@"one",@"two",@"three"]];
NSString *stopKey =
@"two";
__block BOOL stopEarly =
NO;
[dic
enumerateKeysAndObjectsUsingBlock:^(id key,
id obj, BOOL *stop) {
NSLog(@"%@,%@",key,obj);
//訪問對象類型變量
if ([key isEqualToString:stopKey]) {
*stop =
YES;
//訪問__block表識的局部類型變量
stopEarly =
YES;
//直接訪問屬性
NSLog(@"self.name = tom");
;
}
}];
//輸出:
2015-06-08 15:19:09.608 NSDictionary[3035:136164] one,1
2015-06-08 15:19:09.609 NSDictionary[3035:136164] two,2
2015-06-08 15:19:09.609 NSDictionary[3035:136164] self.name = tom
//20.同上一樣利用block對字典進行遍歷,不過加了排序的順序選項options正反序
- (void)enumerateKeysAndObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void
(^)(id key, id obj,
BOOL *stop))block
NS_AVAILABLE(10_6,
4_0);
//21.和第16一樣都是利用value對keys進行排序,只不過這個加上了一個可設定的NSComparato參數條件來比較
- (NSArray *)keysSortedByValueUsingComparator:(NSComparator)cmptr
NS_AVAILABLE(10_6,
4_0);
//例子
NSArray *sortedKeys = [dic
keysSortedByValueUsingComparator:^NSComparisonResult(id obj1,
id obj2) {
if ([obj1 integerValue] > [obj2
integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
if ([obj1 integerValue] < [obj2
integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
return (NSComparisonResult)NSOrderedSame;
}];
NSLog(@"利用keysSortedByValueUsingComparator進行排序%@",sortedKeys);
//輸出:
2015-06-08 16:07:12.361 NSDictionary[3420:160942]
利用keysSortedByValueUsingComparator進行排序(
one,
three,
two
)
//22.通過values對字典的keys進行排序,可以有排序的選擇,還可添加設定的NSComparato參數條件來比較
- (NSArray *)keysSortedByValueWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr
NS_AVAILABLE(10_6,
4_0);
//23.這是一個很好的對字典進行過濾的方法,返回keys的集合,這些keys符合參數block的約束,在block內部在特定的條件下返回yes,返回的這個集合會保留當前遍歷到那個字典對象的信息
- (NSSet *)keysOfEntriesPassingTest:(BOOL (^)(id key,
id obj, BOOL *stop))predicate
NS_AVAILABLE(10_6,
4_0);
//例子:
NSDictionary * numsDic =
@{@(2):@"second",
@(4):@"first",@(1):@"thrid"};
NSSet * filteredKeys = [numsDic
keysOfEntriesPassingTest:^BOOL(id key,
id obj, BOOL *stop) {
BOOL result = NO;
NSNumber * numKey = key;
if (numKey.integerValue >
2) {
result =
YES;
}
return YES;
}];
NSLog(@"filteredKeys.description----%@",filteredKeys.description);
//打印:
2015-06-08 17:34:37.741 NSDictionary[4085:193311] filteredKeys.description----{(
4
)}
//23.用法同上,增加了一個列舉的選項選擇
- (NSSet *)keysOfEntriesWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL
(^)(id key, id obj,
BOOL *stop))predicate
NS_AVAILABLE(10_6,
4_0);
@end
@interface NSDictionary (NSDictionaryCreation)
//24.快速創建一個空字典
+ (instancetype)dictionary;
//25.快速創建字典並且賦初值
+ (instancetype)dictionaryWithObject:(id)object forKey:(id <NSCopying>)key;
//26.條件編譯
不同情況創建字典的幾種方法
#if TARGET_OS_WIN32
+ (instancetype)dictionaryWithObjects:(const
id [])objects forKeys:(const
id [])keys count:(NSUInteger)cnt;
#else
+ (instancetype)dictionaryWithObjects:(const
id [])objects forKeys:(const
id <NSCopying> [])keys count:(NSUInteger)cnt;
#endif
+ (instancetype)dictionaryWithObjectsAndKeys:(id)firstObject, ...
NS_REQUIRES_NIL_TERMINATION;
//27.創建新字典 賦值一個字典
+ (instancetype)dictionaryWithDictionary:(NSDictionary *)dict;
//28.創建字典,通過數組賦值values和keys
+ (instancetype)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray
*)keys;
//29.使用指定的以nil為結尾的對象與鍵對列表初始化列表
- (instancetype)initWithObjectsAndKeys:(id)firstObject, ...
NS_REQUIRES_NIL_TERMINATION;
//30.使用另一個字典初始化字典
- (instancetype)initWithDictionary:(NSDictionary *)otherDictionary;
//31.使用另一個字典初始化字典,還可以為每個對象創建新的副本
- (instancetype)initWithDictionary:(NSDictionary *)otherDictionary copyItems:(BOOL)flag;
//32.使用指定的對象與鍵初始化字典
- (instancetype)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;
//33.使用本地文件的內容初始化字典
+ (NSDictionary *)dictionaryWithContentsOfFile:(NSString *)path;
//34.使用URL的內容初始化字典
+ (NSDictionary *)dictionaryWithContentsOfURL:(NSURL *)url;
//35.使用本地文件的內容初始化字典
- (NSDictionary *)initWithContentsOfFile:(NSString *)path;
//36.使用URL的內容初始化字典
- (NSDictionary *)initWithContentsOfURL:(NSURL *)url;
@end
/**************** Mutable Dictionary
****************/
@interface NSMutableDictionary :
NSDictionary
//37.根據對應的key刪除對應的value以及自身的key,
- (void)removeObjectForKey:(id)aKey;
//38.在可變字典中,改變對應的key的value
- (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey;
//39.創建字典初始化
- (instancetype)init
NS_DESIGNATED_INITIALIZER;
//40.初始化字典並且指定大小
- (instancetype)initWithCapacity:(NSUInteger)numItems
NS_DESIGNATED_INITIALIZER;
//41.序列化對象
- (instancetype)initWithCoder:(NSCoder *)aDecoder
NS_DESIGNATED_INITIALIZER;
@end
@interface NSMutableDictionary (NSExtendedMutableDictionary)
//42.一個字典整體拼接另一個字典的方法
- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary;
//43.刪除字典所有的數據
- (void)removeAllObjects;
//44.根據指定的數據keys刪除對應的values
- (void)removeObjectsForKeys:(NSArray *)keyArray;
//45.給可變字典添加一組新字典
- (void)setDictionary:(NSDictionary *)otherDictionary;
//46.以數組下標的形式來訪問相應鍵的值
- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key
NS_AVAILABLE(10_8,
6_0);
@end
@interface NSMutableDictionary (NSMutableDictionaryCreation)
//47.快速創建可變字典並且初始化大小
+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems;
//48.快速創建可變字典通過指定的文件路徑
+ (NSMutableDictionary *)dictionaryWithContentsOfFile:(NSString *)path;
//49.快速創建可變字典通過URL
+ (NSMutableDictionary *)dictionaryWithContentsOfURL:(NSURL *)url;
//50.使用本地文件的內容創建可變字典
- (NSMutableDictionary *)initWithContentsOfFile:(NSString *)path;
//51.使用URL的內容創建可變字典
- (NSMutableDictionary *)initWithContentsOfURL:(NSURL *)url;
@end
@interface NSDictionary (NSSharedKeySetDictionary)
//52.用來創建預訂好的key集合,返回的值作為NSMutableDictionary的dictionaryWithSharesKeySet參數傳入,可以重用key,從而節約copy操作,節省內存。
+ (id)sharedKeySetForKeys:(NSArray *)keys
NS_AVAILABLE(10_8,
6_0);
@end
@interface NSMutableDictionary (NSSharedKeySetDictionary)
//53.創建字典,共享鍵集會復用對象
+ (NSMutableDictionary *)dictionaryWithSharedKeySet:(id)keyset
NS_AVAILABLE(10_8,
6_0);
@end