Objective-C之Set

介紹

Java有Map,能夠把數據以鍵值對的形式儲存起來,取值的時候經過key就能夠直接拿到對應的值,方便快捷。在Objective-C語言中,詞典就是作這樣的事情的,和NSArray同樣,一個詞典對象也能保存不一樣類型的值,詞典也分別有不可變詞典和可變的詞典(NSDictionary與NSMutableDictionary),前者是線程安全的,後者不是 。安全

字典

NSDictionary不可變字典

  • [NSDictionary dictionaryWithObjectsAndKeys:..] : 使用鍵值對直接建立詞典對象,結尾必需使用nil標誌結束。spa

void doNSDictionary() {
    
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"25",@"age",@"liuguoquan",@"name",@"male",@"sex", nil];
    NSLog(@"dict count: %ld",[dict count]);
    
    //迭代遍歷
    NSEnumerator *enmuerator = [dict keyEnumerator];
    id obj;
    while (obj = [enmuerator nextObject]) {
        NSLog(@"字典元素: %@",obj);
    }
    
    //全部鍵值集合
    NSArray *key = [dict allKeys];
    for(NSObject *object in key) {
        NSLog(@"值: %@.",[dict objectForKey:object]);
    }
    
}

2016-10-10 11:32:22.856639 DictionaryDemo[17399:1378107] dict count: 3
2016-10-10 11:32:22.857286 DictionaryDemo[17399:1378107] 字典元素: age
2016-10-10 11:32:22.857317 DictionaryDemo[17399:1378107] 字典元素: name
2016-10-10 11:32:22.857330 DictionaryDemo[17399:1378107] 字典元素: sex

2016-10-10 11:32:22.857372 DictionaryDemo[17399:1378107] 值: 25.
2016-10-10 11:32:22.857390 DictionaryDemo[17399:1378107] 值: liuguoquan.
2016-10-10 11:32:22.857404 DictionaryDemo[17399:1378107] 值: male.

NSMutableDictionary可變字典

NSMutableDictionary是NSDictionary的子類,因此繼承了NSDictionary的方法, 以上的代碼對NSMutableDictionary來講徹底可用。下面來看看不同的增刪鍵值數據線程

  • [dictionary setObject: forKey:] :向可變的詞典動態的添加數據code

  • [dictionary removeAllObjects..] : 刪除掉詞典中的全部數據。對象

  • [dictionary removeObjectForKey..] :刪除掉詞典中指定key的數據繼承

void doNSMutableDictionary() {
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"25",@"age",@"張三",@"name",@"男",@"性別",nil];
    //增長鍵值對
    [dictionary setObject:@"30名" forKey:@"名次"];
    
    NSLog(@"mutDict count: %lu", [dictionary count]);
    NSEnumerator *enumeratorKey = [dictionary keyEnumerator];
    for (NSObject *object in enumeratorKey) {
        NSLog(@"key:%@", object);
    }
    
    NSEnumerator *enumeratorObject = [dictionary objectEnumerator];
    for (NSObject *object in enumeratorObject) {
        NSLog(@"value:%@", object);
    }
    NSLog(@"key 名次的值是:%@", [dictionary objectForKey:@"名次"]);
    [dictionary removeObjectForKey:@"名次"];
    NSLog(@"%lu", [dictionary count]);
}

2016-10-10 11:32:22.857428 DictionaryDemo[17399:1378107] mutDict count: 4
2016-10-10 11:32:22.857460 DictionaryDemo[17399:1378107] key:age
2016-10-10 11:32:22.857485 DictionaryDemo[17399:1378107] key:性別
2016-10-10 11:32:22.857497 DictionaryDemo[17399:1378107] key:name
2016-10-10 11:32:22.857508 DictionaryDemo[17399:1378107] key:名次

2016-10-10 11:32:22.857533 DictionaryDemo[17399:1378107] value:25
2016-10-10 11:32:22.857543 DictionaryDemo[17399:1378107] value:男
2016-10-10 11:32:22.857552 DictionaryDemo[17399:1378107] value:張三
2016-10-10 11:32:22.857561 DictionaryDemo[17399:1378107] value:30名

2016-10-10 11:32:22.857594 DictionaryDemo[17399:1378107] key 名次的值是:30名
2016-10-10 11:32:22.857635 DictionaryDemo[17399:1378107] 3
相關文章
相關標籤/搜索