Objective C之NSDictionary學習筆記(IOS 9 1)

半紙淵發表於2017-12-14

NSDictionary類簡介

 1. 以key-object的形式儲存資料,是一個集合類(collection)
 2. 詞典中詞條的儲存是無序的
 3. 不可變詞典(內容一旦init後就不能更改)
 4. 既然不能更改,當然就不能進行刪除、替換、增加操作,只能查詢
 5. key值不能重複
複製程式碼

屬性表(@property)

@property

描述

@property(readonly) NSUInteger count 詞典詞條的 數量
@property(readonly, copy) NSArray < ObjectType > *allValues 詞典的所有 詞條(陣列)
@property(readonly, copy) NSString *description 描述詞典內容的字串(plist格式)
@property(readonly, copy) NSString *descriptionInStringsFileFormat 描述.string格式檔案的詞典內容的字串

常用方法

常用類方法

詞典物件的生成

+ (instancetype)dictionary // 生成並返回一個空的詞典物件
複製程式碼
+ (instancetype)dictionaryWithObject:(ObjectType)anObject forKey:(id<NSCopying>)aKey // 根據akey和anObject返回一個詞典物件 
複製程式碼

常用例項方法

詞典物件的初始化

``` - (instancetype)initWithObjects:(NSArray *)objects forKeys:(NSArray> *)keys //分別從objects和keys陣列中取出一個元素作為 key - object 鍵值對,並返回包含該鍵值對的詞典物件 ``` >便利構造器:dictionaryWithObjects : forKeys :
- (instancetype)initWithObjects:(const ObjectType _Nonnull
[])objects forKeys:(const id<NSCopying> _Nonnull [])keys
count:(NSUInteger)count	//用objects和keys陣列的元素來生成count條詞條的詞典物件
複製程式碼

便利構造器:dictionaryWithObjects : forKeys : count :


- (instancetype)initWithObjectsAndKeys:(id)firstObject, ... //使用指定的object - key來建立詞典物件,以nil作為結束
複製程式碼

便利構造器:dictionaryWithObjectsAndKeys :


- (instancetype)initWithDictionary:
(NSDictionary<KeyType,ObjectType> *)otherDictionary //用一個已經存在的詞典物件建立一個詞典物件(注:此處的otherDictionary可以是可變詞典物件)
複製程式碼

便利構造器:dictionaryWithDictionary :


詞典物件的訪問

- (NSUInteger) count //返回詞條數量
複製程式碼
- (ObjectType)objectForKey:(KeyType)aKey //返回akey對應的值,若不存在則返回nil
複製程式碼
- (NSArray <KeyType> *)allKeys //返回詞典中所有物件的key
複製程式碼
- (NSArray <KeyType> *)allValues //返回詞典中所有的值物件
複製程式碼
- (NSEnumerator<ObjectType> *)keyEnumerator //返回一個可以快速訪問詞典中所有關鍵字的 快速列舉器
複製程式碼
- (NSEnumerator<ObjectType> *)objectEnumerator //返回一個可以快速訪問詞典中所有值物件的 快速列舉器
複製程式碼
- (NSArray<KeyType> *)allKeysForObject:(ObjectType)anObject //返回詞典中所有值物件為anObject的key陣列
複製程式碼

詞典物件的比較

- (BOOL)isEqualToDictionary:
(NSDictionary<KeyType,ObjectType> *)otherDictionary //如果兩個詞典的詞條數和key - object都是一樣就返回YES
複製程式碼

詞典物件,檔案輸入與輸出

- (NSString *)description //以ASCII編碼的屬性列表格式輸出詞典的詞條
複製程式碼

- (NSDictionary<KeyType,ObjectType>*)initWithContentsOfFile:(NSString *)path //根據屬性列表格式儲存的檔案來初始化詞典物件
複製程式碼

便利構造器:dictionaryWithContentsOfFile:


- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag //把代表這個詞典內容的屬性列表輸出到指定的檔案(flag是控制寫入的,如果為YES則表示完全寫入)
複製程式碼

參考方法:writeToFile: atomically: encoding: error:


詞典物件的遍歷

for (... in ...) {
   ...
}
複製程式碼
- (void)enumerateKeysAndObjectsUsingBlock:(void (^)(KeyType
key, ObjectType obj, BOOL *stop))block 
複製程式碼

詞典物件,小試牛刀

//Xcode 7.2
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        //1.詞典物件的生成
        NSDictionary *dict = [NSDictionary dictionary]; //返回空的詞典物件
        NSLog(@"dict --> %@",[dict description]);    //列印物件的內容,返回一個字串“{ 內容在這 }”
        //1.1 返回一個以dict - dict1(key - object)的詞典物件
        NSDictionary *dict1 = [NSDictionary dictionaryWithObject:@"dict1" forKey:@"dict"];
        NSLog(@"dict1 --> %@",[dict1 description]);
        //1.2 返回一個以dict - 1(key - object)的詞典物件 (@1,是指NSValue 1)
        NSDictionary *dict1_1 = [NSDictionary dictionaryWithObject:@1 forKey:@"dict"];
        NSLog(@"dict1_1 --> %@",[dict1_1 description]);
        
        //2.詞典物件的初始化
        //2.1 返回一個以@[@"-1-",@"-2-",@"-3-"] - @[@1,@2,@3]為基礎逐一建立的,“-1-” - 1,“-2-” - 2,“-3-” - 3共三條詞條的詞典物件
        NSDictionary *dict2 = [[NSDictionary alloc] initWithObjects:@[@1,@2,@3] forKeys:@[@"-1-",@"-2-",@"-3-"]];
        NSLog(@"dict2 --> %@",[dict2 description]);
        //2.2 IOS 8可以用,但IOS 9才行被搞死了 T_T
//        __unsafe_unretained NSArray * _Nonnull arryObjects = @[@"one",@"two",@"三"];
//        __unsafe_unretained NSArray * _Nonnull arrayKeys = @[@1,@2,@3];
//        NSDictionary *dict2_1 = [[NSDictionary alloc] initWithObjects:arryObjects forKeys:arrayKeys count:4];
//        NSLog(@"dict2_1 --> %@",[dict2_1 description]);
        //報錯資訊:Implicit conversion of an Objective-C pointer to 'const id  _Nonnull __unsafe_unretained *' is disallowed with ARC
        //2.3
        NSDictionary *dict2_2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"object1", @"key1", @"object2", @"key2", nil];
        NSLog(@"dict2_2 --> %@",[dict2_2 description]);
        //2.4
        NSDictionary *dict2_3 = [[NSDictionary alloc] initWithDictionary:dict2_2];
        NSLog(@"dict2_3 --> %@",[dict2_3 description]);
        
        //3.詞典物件的訪問
        NSDictionary *dict3 = @{@1 : @"one", @2 : @"two", @3 : @[@"three",@"four"], @4 : @"one"};
        //3.1 詞條的數量
        NSLog(@"dict3 --> %@,詞條數:%lu",[dict3 description], [dict3 count]);
        //3.2 根據 key 尋找 object
        NSLog(@"dict3 --> %@", [dict3 objectForKey:@2]);
        NSLog(@"dict3 --> %@", [dict3 objectForKey:@3]);
        //3.3 返回包含所有key的陣列
        NSArray *arryAllKeys = [dict3 allKeys];
        for (NSNumber *key in arryAllKeys) {
            NSLog(@"Allkeys --> %@", [key description]);
        }
        //3.4 返回一個列舉器
        NSEnumerator * enumer = [dict3 keyEnumerator];
        for (NSNumber *key in enumer) {
            NSLog(@"enumerkeys --> %@", [key description]);
        }
        //3.5 返回一個包含@“one”的所有關鍵字的陣列
        NSArray *arryOneKeys = [dict3 allKeysForObject:@"one"];
        for (NSNumber *key in arryOneKeys) {
            NSLog(@"onekeys --> %@", [key description]);
        }
        
        //4.詞典物件的比較
        NSDictionary *dict4_1 = @{@1 : @"one", @2 : @"two", @3 : @"three"};
        NSDictionary *dict4_2 = @{@1 : @"one", @2 : @"two", @3 : @"three"};
        NSDictionary *dict4_3 = @{@1 : @"one", @2 : @"two"};
        NSDictionary *dict4_4 = @{@1 : @"一", @2 : @"二", @3 : @"三"};
        //只有key和object完全一樣才會返回YES
        [dict4_1 isEqualToDictionary:dict4_2] ? NSLog(@"dict4_1 & dict4_2: YES"): NSLog(@"dict4_1 & dict4_2: NO");
        [dict4_1 isEqualToDictionary:dict4_3] ? NSLog(@"dict4_1 & dict4_3: YES"): NSLog(@"dict4_1 & dict4_3: NO");
        [dict4_3 isEqualToDictionary:dict4_4] ? NSLog(@"dict4_3 & dict4_4: YES"): NSLog(@"dict4_3 & dict4_4: NO");
        
        //5.詞典物件與檔案操作
        //5.1 根據plist檔案來建立詞典
        NSDictionary *dict5 = [[NSDictionary alloc] initWithContentsOfFile:@"/Users/windy/Desktop/OC pros/NSDictionaryTest/dict.plist"];
        NSLog(@"dict5 --> %@", [dict5 description]);
        //5.2 把詞典詞條寫進檔案
        NSDictionary *dict5_1 = @{@"一" : @[@"one",@"one one"], @"二" : @3};
        [dict5_1 writeToFile:@"/Users/windy/Desktop/OC pros/NSDictionaryTest/dict2.plist" atomically:YES];
        //5.3 把詞典詞條讀取出來,使用的是initWithContentsOfFile:的便利構造器dictionaryWithContentsOfFile:
        NSDictionary *dict5_2 = [[NSDictionary alloc] init];
        dict5_2 = [NSDictionary dictionaryWithContentsOfFile:@"/Users/windy/Desktop/OC pros/NSDictionaryTest/dict2.plist"];
        NSLog(@"dict5_2 --> %@", [dict5_2 description]);
        
        //6.利用block遍歷詞典
        NSDictionary *dict6 = @{@"china" : @"中國", @"Eng" : @"英國"};
        [dict6 enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            NSLog(@"dict6 --> key - obj: %@ = %@", key, obj);
        }];
    }
    return 0;
}
複製程式碼

相關文章