Set of Objective-C

Time:2021-6-28

introduce

Java has map, which can store data in the form of key value pairs. When taking values, you can get the corresponding values directly through the key, which is convenient and fast. In Objective-C language, dictionaries do such things. Like nsarray, a dictionary object can store different types of values. Dictionaries also have immutable dictionaries and variable dictionaries (nsdictionary and nsmutabledictionary). The former is thread safe, while the latter is not.

Set of Objective-C

Nsdictionary immutable dictionary

  • [NSDictionary dictionaryWithObjectsAndK eys:.. ]: use key value pair to create dictionary object directly. The end must use nil flag.

void doNSDictionary() {
    
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"25",@"age",@"liuguoquan",@"name",@"male",@"sex", nil];
    NSLog(@"dict count: %ld",[dict count]);
    
    //Iterative traversal
    NSEnumerator *enmuerator = [dict keyEnumerator];
    id obj;
    while (obj = [enmuerator nextObject]) {
        Nslog (@ "dictionary element:% @", obj));
    }
    
    //All key value sets
    NSArray *key = [dict allKeys];
    for(NSObject *object in key) {
        Nslog (@ "value:% @", [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] dictionary element: age
2016-10-10 11:32:22.857317 dictionarydemo [17399:1378107] dictionary element: name
2016-10-10 11:32:22.857330 dictionarydemo [17399:1378107] dictionary element: sex

2016-10-10 11:32:22.857372 dictionarydemo [17399:1378107] value: 25
2016-10-10 11:32:22.857390 dictionarydemo [17399:1378107] value: liuguoquan
2016-10-10 11:32:22.857404 dictionary demo [17399:1378107] value: male

Nsmutabledictionary variable dictionary

Nsmutabledictionary is a subclass of nsdictionary, so it inherits the methods of nsdictionary. The above code is completely available for nsmutabledictionary. Let’s take a look at different data of adding and deleting key values

  • [dictionary setobject: forkey]: dynamically add data to a variable dictionary

  • [dictionary removeallobjects..]: delete all the data in the dictionary.

  • [dictionary removeobjectforkey..]: delete the data of the specified key in the dictionary

void doNSMutableDictionary() {
    Nsmutabledictionary * dictionary = [nsmutabledictionary withobjectsandkeys: @ "25", @ "age", @ "Zhang San", @ "name", @ "male", @ "gender", nil];
    //Add key value pair
    [dictionary setobject: @ "30 names" for key: @ "rank];
    
    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 rank value is:% @, [dictionary objectforkey: @" rank]);
    [dictionary removeobjectforkey: @ "rank];
    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: Gender
2016-10-10 11:32:22.857497 DictionaryDemo[17399:1378107] key:name
2016-10-10 11:32:22.857508 dictionarydemo [17399:1378107] key: ranking

2016-10-10 11:32:22.857533 DictionaryDemo[17399:1378107] value:25
2016-10-10 11:32:22.857543 dictionarydemo [17399:1378107] value: Male
2016-10-10 11:32:22.857552 dictionarydemo [17399:1378107] value: Zhang San
2016-10-10 11:32:22.857561 DictionaryDemo[17399:1378107]  value:30 name

2016-10-10 11:32:22.857594 dictionarydemo [17399:1378107] the value of key position is: 30
2016-10-10 11:32:22.857635 DictionaryDemo[17399:1378107] 3