NSDictionary 保存具有映射關(guān)系的數(shù)據(jù),一組key 另一組 value Map的key不可重復(fù)
key和value都可以是任何引用類型的數(shù)據(jù)
key和value存在單項一對一 ?指定key就能找到唯一的,確定的value
allKeys 方法 返回所有key組成集合 返回值為NSArray類型
7.8.1NSDictionary的功能和用法
以dictionary開始的類方法和以init開頭的實例方法
1.dictionary: 創(chuàng)建空字典
2、dictionaryWithContentsOfFile:、initWithContentsOfFile:讀取指定文件內(nèi)容,使用指定文件初始化 通常為dictionary自己輸出形成
3.dictinaryWithDictionary:/initWithDictionary:使用已有的NSDictionary
4.dictionaryWithObject:forKey:使用單個key_value
5.dictionaryWithObjects:forKeys:/initWithObjects:forKeys:使用兩個NSArray分別制定key、value集合,以達到創(chuàng)建多組的目的
6.dictionary:withOBjectsAndKeys:/initWithObjectsAndKeys:需要按照 value1,key1,value2,key2,...,nil的格式傳入
訪問字典:
1.count: 所包含的KV對個數(shù)
2.allKeys
3.allKeysForObject:指定value對應(yīng)的全部key
4.allValues:NSDictionary包含的全部value
5、objectForKey:指定key對應(yīng)value
6、objectForKeyedSubscript:通過該方法支持,允許通過下表發(fā)獲取指定K對應(yīng)V
7.keyEnumerator:key遍歷
8.objectEnumerator:value遍歷
9.enumerateKeysAndObjectsUsingBlock:迭代
10.writeToFile:atomically:寫入指定文件
NSDictionary* dict=[NSDictionary
dictionaryWithObjectsAndKeys:
@"希羅多德",@"xi",
@"亞里士多德",@"ya",
@"蘇格拉底",@"su",nil];
[dict print];
NSLog(@"ditc包含%ld個key-value對",[dict count]);
NSLog(@"dict的所有Key為:%@",
[dict allKeys]);
NSLog(@"dict的所有 value為:%@",
[dict allValues]);
// 遍歷
//枚舉器
NSObject* value;
NSEnumerator * en=[dict objectEnumerator];
while (value=[en nextObject]) {
NSLog(@"%@",value);
}
//方法
[dict enumerateKeysAndObjectsUsingBlock:^(id? _Nonnull key, id? _Nonnull obj, BOOL * _Nonnull stop) {
NSLog(@"%@",obj);
}];
7.8.2對NSDictionary 的key排序
對key的排序,返回的是所有key組成的NSArray:
1、keysSortedByValueUsingSelector:根據(jù)NSictionary的所有value的指定方法返回值對Key排序:調(diào)用value的該方法必須返回NSOrderedAscending/NSOreredDescending/NOreredSame三個之一
2、keysSortedByValueUsingComparator:該方法使用指定的代碼塊遍歷KV對,返回三個值之一
3.keysSortedByValueWithOptions:usingComparator:可傳入額外的NSEnumerationOptions
NSOrderedAscending/遞增
NSOreredDescending/遞減
NOreredSame
value對應(yīng)的字符串越長,value對應(yīng)的字越大
NSDictionary* dict=[NSDictionary
dictionaryWithObjectsAndKeys:
@"希羅多德",@"xi",
@"亞里士多德",@"ya",
@"蘇格拉底",@"su",nil];
[dict print];
NSArray* arr1=[dict keysSortedByValueUsingSelector:
@selector(compare:)];
NSArray* arr2=[dict keysSortedByValueUsingComparator:^NSComparisonResult(id? _Nonnull obj1, id? _Nonnull obj2) {
if ([obj1 length]? >[obj2 length]) {
return? NSOrderedDescending;
}else if ([obj1 length]<[obj2 length]){
return NSOrderedAscending;
}else{
return NSOrderedSame;
}
//? ? ? ? ? ? [arr2 writeToFile:@"myFile.txt" atomically:YES];
//? ? ? ? ? ? NSArray* arr3=[arr3]
NSLog(@"%@",arr2);
7.8.3 對NSDictionary 的key進行過濾
所有key執(zhí)行過濾, 返回NSSet
keysOfEntriesPassingTest:使用代碼塊迭代處理NSDictionary的每個key-value對。代碼塊必須返回BOOL類型的值,只有單改代碼返回YES時,該key才會被保留下來,可以接受三個參數(shù)。
第一個參數(shù):正在迭代處理的key
第二個參數(shù):迭代處理的value
第三個參數(shù):還需要繼續(xù)迭代 如果將第三個采納數(shù)設(shè)置為NO 該方法會立即停止迭代
keysOfEnteriesWithOptions:passingTest:
額外附加一個NSEnumerationOptions的參數(shù)
NSSet* set=[dic keysOfEntriesPassingTest:^BOOL(id? _Nonnull key, id? _Nonnull obj, BOOL * _Nonnull stop) {
return (BOOL)([obj integerValue]>65);
}];
7.8.4 使用自定義類作為NSDictionary的key
必須滿足:
1.該自定義類正確重寫過isEqual:和hash方法
2.實現(xiàn)了copyWithZone方法,該方法最好能返回對象的不可變副本
建議實現(xiàn)NScopying協(xié)議
為了防止key值被修改導(dǎo)致的NSDictionary完整性被破壞,NSDictionary只要程序把任何對象都作為key放入NSDictionary時,NSDictionary總會先調(diào)用該Key的copy方法復(fù)制對象的不可變副本,然后使用該副本作為NSDictionary的key
-(id)copyWithZone:(NSZone*)zone
{
NSLog(@"——正在復(fù)制——");
//復(fù)制一個對象
FKUser* newUser=[[self class] allocWithZone:zone] init];
//將被復(fù)制對象的實變量的值賦給新對象的實例變量
newUser->name=name;
newUser->pass=pass;
return newUser;
}
#improt
#import "NSDictionary+print.h"
#import "FKUser.h"
int main(int argc,char * argv[])
{
@autoreleasepool{
FKUser* ul=[[FKUser alloc] initWithName;@“bai”
pass;@"345"];
//直接使用多個value,key的形式創(chuàng)建NSDIcitonary對象
NSDicitonary* dict=[NSDictionary
dictionaryWithObjectsAndKeys:
@"one",[FKUser alloc] initWithName:@"sun"
pass:@"123"],
@"two",u1,
@“three”,[[FKUser alloc] initWithName:@"sun"
pass:@"123"],
@“four,[[FKUser alloc] initWithName:@"tang"
pass:@"178"],nil];
];
}
}
7.8.5 NSMutableDicitonary的功能
類似NSMutableArray 與NSArray
1.setObject:forKey:新增或覆蓋已有的
2.setObject:forKeyedSubscript: 通過該方法的支持,允許程序通過下標法設(shè)置KV對
3.addEntriesFromDictionary:將另一個NSDictionnary中所有的KV對復(fù)制到NSDIctionary中
4.setDicitonary: 替換
5.removerObjectForKey:根據(jù)key來刪除KV對
6.removeAllObjects
7.removeObjectsForKeys:使用多個key組成的NSArray作為參數(shù),同時刪除多個Key對應(yīng)KV對