NSDictionary所有API的学习。
<歡迎大家增加iOS開發學習交流群:QQ529560119>
@property (readonly)NSUInteger count;
//1.利用指定的key尋找相應的value
- (id)objectForKey:(id)aKey;
//2. keyEnumerator得到一個字典的全部鍵值
- (NSEnumerator *)keyEnumerator;
//3.初始化字典
- (instancetype)initNS_DESIGNATED_INITIALIZER;
//4.條件編譯依據不同情況來初始化字典
#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.數組全部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=[NSDictionarydictionaryWithObjectsAndKeys:@"K1",@"V1",@"K2",@"V2",@"K3",@"V3",nil];
? ? //NSArray *arr1=[NSArrayarrayWithObjects:@"V1",@"V2",@"VG",nil];
? ? //NSArray *ARR= [dicobjectsForKeys: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=[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對字典進行遍歷
- (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);
? ? ? ? //訪問對象類型變量
? ? ? ?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)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進行排序%@",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)cmptrNS_AVAILABLE(10_6,4_0);
//23.這是一個非常好的對字典進行過濾的方法,返回keys的集合,這些keys符合參數block的約束,在block內部在特定的條件下返回yes,返回的這個集合會保留當前遍歷到那個字典對象的信息
- (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.使用方法同上,添加了一個列舉的選項選擇
- (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:(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.創建新字典 賦值一個字典
+ (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)initNS_DESIGNATED_INITIALIZER;
//40.初始化字典而且指定大小
- (instancetype)initWithCapacity:(NSUInteger)numItemsNS_DESIGNATED_INITIALIZER;
//41.序列化對象
- (instancetype)initWithCoder:(NSCoder *)aDecoderNS_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>)keyNS_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 *)keysNS_AVAILABLE(10_8,6_0);
@end
@interface NSMutableDictionary (NSSharedKeySetDictionary)
//53.創建字典,共享鍵集會復用對象
+ (NSMutableDictionary *)dictionaryWithSharedKeySet:(id)keysetNS_AVAILABLE(10_8,6_0);
@end
總結
以上是生活随笔為你收集整理的NSDictionary所有API的学习。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android SQLiteDataba
- 下一篇: 使用infinite-scroll实现G