NSDictionary所有API的学习。
<歡迎大家增加iOS開(kāi)發(fā)學(xué)習(xí)交流群:QQ529560119>
@property (readonly)NSUInteger count;
//1.利用指定的key尋找相應(yīng)的value
- (id)objectForKey:(id)aKey;
//2. keyEnumerator得到一個(gè)字典的全部鍵值
- (NSEnumerator *)keyEnumerator;
//3.初始化字典
- (instancetype)initNS_DESIGNATED_INITIALIZER;
//4.條件編譯依據(jù)不同情況來(lái)初始化字典
#if TARGET_OS_WIN32
- (instancetype)initWithObjects:(constid [])objects forKeys:(constid [])keys count:(NSUInteger)cnt;
#else
- (instancetype)initWithObjects:(constid [])objects forKeys:(constid <NSCopying> [])keys count:(NSUInteger)cntNS_DESIGNATED_INITIALIZER;
#endif
- (instancetype)initWithCoder:(NSCoder *)aDecoderNS_DESIGNATED_INITIALIZER;
@end
@interface NSDictionary (NSExtendedDictionary)
//5.數(shù)組全部key屬性
@property (readonly,copy) NSArray *allKeys;
//6.依據(jù)所填入的object返回相應(yīng)全部的key鍵值
- (NSArray *)allKeysForObject:(id)anObject;
//7.屬性 字典全部value ?
@property (readonly,copy) NSArray *allValues;
//8.屬性 字符串描寫(xiě)敘述
@property (readonly,copy) NSString *description;
//9.屬性 字符串描寫(xiě)敘述文件格式
@property (readonly,copy) NSString *descriptionInStringsFileFormat;
//10.依據(jù)設(shè)置的locale進(jìn)行連接數(shù)組
- (NSString *)descriptionWithLocale:(id)locale;
//11.依據(jù)設(shè)置的locale進(jìn)行連接數(shù)組
- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level;
//12.推斷字典是否相等
- (BOOL)isEqualToDictionary:(NSDictionary *)otherDictionary;
//13.得到一個(gè)字典的全部values
- (NSEnumerator *)objectEnumerator;
//14.字典將某個(gè)特定的數(shù)組作為key值傳進(jìn)去得到相應(yīng)的value,假設(shè)某個(gè)key找不到相應(yīng)的key,就用notFoundMarker提前設(shè)定的值取代
- (NSArray *)objectsForKeys:(NSArray *)keys notFoundMarker:(id)marker;
? ? //NSDictionary *dic=[NSDictionarydictionaryWithObjectsAndKeys:@"K1",@"V1",@"K2",@"V2",@"K3",@"V3",nil];
? ? //NSArray *arr1=[NSArrayarrayWithObjects:@"V1",@"V2",@"VG",nil];
? ? //NSArray *ARR= [dicobjectsForKeys:arr1 notFoundMarker:@"BB"];
? ? //NSLog(@"測(cè)試測(cè)試%@",ARR);
? ? //打印:
? ? //2015-06-08 11:30:54.139 NSDictionary[1624:64989]測(cè)試測(cè)試(
? ?//K1,
? ?//BB,
? ?//BB
? ? //)
//15.將字典寫(xiě)進(jìn)特定的路徑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的大小順序來(lái)對(duì)keys鍵值進(jìn)行排序(通過(guò)value排序,返回key集合)
- (NSArray *)keysSortedByValueUsingSelector:(SEL)comparator;
//NSDictionary *dic1=[NSDictionarydictionaryWithObjectsAndKeys:@"4",@"A",@"6",@"C",@"5",@"B",nil];
? ? //NSArray *arr2= [dic1keysSortedByValueUsingSelector:@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)keyNS_AVAILABLE(10_8,6_0);
//19.利用block對(duì)字典進(jìn)行遍歷
- (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id key,id obj, BOOL *stop))blockNS_AVAILABLE(10_6,4_0);
//樣例:
? ? NSDictionary *dic = [NSDictionarydictionaryWithObjects:@[@"1",@"2",@"3"]forKeys:@[@"one",@"two",@"three"]];
? ?NSString *stopKey = @"two";
? ?__block BOOL stopEarly =NO;
? ? [dicenumerateKeysAndObjectsUsingBlock:^(id key,id obj, BOOL *stop) {
? ? ? ?NSLog(@"%@,%@",key,obj);
? ? ? ? //訪問(wèn)對(duì)象類(lèi)型變量
? ? ? ?if ([key isEqualToString:stopKey]) {
? ? ? ? ? ? *stop =YES;
? ? ? ? ? ? //訪問(wèn)__block表識(shí)的局部類(lèi)型變量
? ? ? ? ? ? stopEarly =YES;
? ? ? ? ? ?//直接訪問(wèn)屬性
? ? ? ? ? ? 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對(duì)字典進(jìn)行遍歷,只是加了排序的順序選項(xiàng)options正反序
- (void)enumerateKeysAndObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(id key, id obj,BOOL *stop))block NS_AVAILABLE(10_6, 4_0);
//21.和第16一樣都是利用value對(duì)keys進(jìn)行排序,僅僅只是這個(gè)加上了一個(gè)可設(shè)定的NSComparato參數(shù)條件來(lái)比較
- (NSArray *)keysSortedByValueUsingComparator:(NSComparator)cmptrNS_AVAILABLE(10_6,4_0);
//樣例
? ?NSArray *sortedKeys = [dic keysSortedByValueUsingComparator:^NSComparisonResult(id obj1,id obj2) {
? ? ? ?if ([obj1 integerValue] > [obj2integerValue]) {
? ? ? ? ? ? return (NSComparisonResult)NSOrderedAscending;
? ? ? ? }
? ? ? ?if ([obj1 integerValue] < [obj2integerValue]) {
? ? ? ? ? ? return (NSComparisonResult)NSOrderedDescending;
? ? ? ? }
? ? ? ? return (NSComparisonResult)NSOrderedSame;
? ? }];
? ? NSLog(@"利用keysSortedByValueUsingComparator進(jìn)行排序%@",sortedKeys);
//輸出:
2015-06-08 16:07:12.361 NSDictionary[3420:160942]利用keysSortedByValueUsingComparator進(jìn)行排序(
? ? one,
? ? three,
? ? two
)
//22.通過(guò)values對(duì)字典的keys進(jìn)行排序。能夠有排序的選擇,還可加入設(shè)定的NSComparato參數(shù)條件來(lái)比較
- (NSArray *)keysSortedByValueWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptrNS_AVAILABLE(10_6,4_0);
//23.這是一個(gè)非常好的對(duì)字典進(jìn)行過(guò)濾的方法,返回keys的集合,這些keys符合參數(shù)block的約束,在block內(nèi)部在特定的條件下返回yes,返回的這個(gè)集合會(huì)保留當(dāng)前遍歷到那個(gè)字典對(duì)象的信息
- (NSSet *)keysOfEntriesPassingTest:(BOOL (^)(id key,id obj, BOOL *stop))predicateNS_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.使用方法同上,添加了一個(gè)列舉的選項(xiàng)選擇
- (NSSet *)keysOfEntriesWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id key, id obj,BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
@end
@interface NSDictionary (NSDictionaryCreation)
//24.高速創(chuàng)建一個(gè)空字典
+ (instancetype)dictionary;
//25.高速創(chuàng)建字典而且賦初值
+ (instancetype)dictionaryWithObject:(id)object forKey:(id <NSCopying>)key;
//26.條件編譯 不同情況創(chuàng)建字典的幾種方法
#if TARGET_OS_WIN32
+ (instancetype)dictionaryWithObjects:(constid [])objects forKeys:(constid [])keys count:(NSUInteger)cnt;
#else
+ (instancetype)dictionaryWithObjects:(constid [])objects forKeys:(constid <NSCopying> [])keys count:(NSUInteger)cnt;
#endif
+ (instancetype)dictionaryWithObjectsAndKeys:(id)firstObject, ...NS_REQUIRES_NIL_TERMINATION;
//27.創(chuàng)建新字典 賦值一個(gè)字典
+ (instancetype)dictionaryWithDictionary:(NSDictionary *)dict;
//28.創(chuàng)建字典。通過(guò)數(shù)組賦值values和keys
+ (instancetype)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;
//29.使用指定的以nil為結(jié)尾的對(duì)象與鍵對(duì)列表初始化列表
- (instancetype)initWithObjectsAndKeys:(id)firstObject, ...NS_REQUIRES_NIL_TERMINATION;
//30.使用還有一個(gè)字典初始化字典
- (instancetype)initWithDictionary:(NSDictionary *)otherDictionary;
//31.使用還有一個(gè)字典初始化字典,還能夠?yàn)槊恳粋€(gè)對(duì)象創(chuàng)建新的副本
- (instancetype)initWithDictionary:(NSDictionary *)otherDictionary copyItems:(BOOL)flag;
//32.使用指定的對(duì)象與鍵初始化字典
- (instancetype)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;
//33.使用本地文件的內(nèi)容初始化字典
+ (NSDictionary *)dictionaryWithContentsOfFile:(NSString *)path;
//34.使用URL的內(nèi)容初始化字典
+ (NSDictionary *)dictionaryWithContentsOfURL:(NSURL *)url;
//35.使用本地文件的內(nèi)容初始化字典
- (NSDictionary *)initWithContentsOfFile:(NSString *)path;
//36.使用URL的內(nèi)容初始化字典
- (NSDictionary *)initWithContentsOfURL:(NSURL *)url;
@end
/**************** Mutable Dictionary****************/
@interface NSMutableDictionary :NSDictionary
//37.依據(jù)相應(yīng)的key刪除相應(yīng)的value以及自身的key,
- (void)removeObjectForKey:(id)aKey;
//38.在可變字典中,改變相應(yīng)的key的value
- (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey;
//39.創(chuàng)建字典初始化
- (instancetype)initNS_DESIGNATED_INITIALIZER;
//40.初始化字典而且指定大小
- (instancetype)initWithCapacity:(NSUInteger)numItemsNS_DESIGNATED_INITIALIZER;
//41.序列化對(duì)象
- (instancetype)initWithCoder:(NSCoder *)aDecoderNS_DESIGNATED_INITIALIZER;
@end
@interface NSMutableDictionary (NSExtendedMutableDictionary)
//42.一個(gè)字典總體拼接還有一個(gè)字典的方法
- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary;
//43.刪除字典全部的數(shù)據(jù)
- (void)removeAllObjects;
//44.依據(jù)指定的數(shù)據(jù)keys刪除相應(yīng)的values
- (void)removeObjectsForKeys:(NSArray *)keyArray;
//45.給可變字典加入一組新字典
- (void)setDictionary:(NSDictionary *)otherDictionary;
//46.以數(shù)組下標(biāo)的形式來(lái)訪問(wèn)對(duì)應(yīng)鍵的值
- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)keyNS_AVAILABLE(10_8,6_0);
@end
@interface NSMutableDictionary (NSMutableDictionaryCreation)
//47.高速創(chuàng)建可變字典而且初始化大小
+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems;
//48.高速創(chuàng)建可變字典通過(guò)指定的文件路徑
+ (NSMutableDictionary *)dictionaryWithContentsOfFile:(NSString *)path;
//49.高速創(chuàng)建可變字典通過(guò)URL
+ (NSMutableDictionary *)dictionaryWithContentsOfURL:(NSURL *)url;
//50.使用本地文件的內(nèi)容創(chuàng)建可變字典
- (NSMutableDictionary *)initWithContentsOfFile:(NSString *)path;
//51.使用URL的內(nèi)容創(chuàng)建可變字典
- (NSMutableDictionary *)initWithContentsOfURL:(NSURL *)url;
@end
@interface NSDictionary (NSSharedKeySetDictionary)
//52.用來(lái)創(chuàng)建預(yù)訂好的key集合。返回的值作為NSMutableDictionary的dictionaryWithSharesKeySet參數(shù)傳入,能夠重用key,從而節(jié)約copy操作,節(jié)省內(nèi)存。
+ (id)sharedKeySetForKeys:(NSArray *)keysNS_AVAILABLE(10_8,6_0);
@end
@interface NSMutableDictionary (NSSharedKeySetDictionary)
//53.創(chuàng)建字典,共享鍵集會(huì)復(fù)用對(duì)象
+ (NSMutableDictionary *)dictionaryWithSharedKeySet:(id)keysetNS_AVAILABLE(10_8,6_0);
@end
總結(jié)
以上是生活随笔為你收集整理的NSDictionary所有API的学习。的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Android SQLiteDataba
- 下一篇: maven 工程启动找不到 Spring