Add values in NSMutableDictionary in iOS with Objective-C

前端 未结 3 1828
死守一世寂寞
死守一世寂寞 2021-02-07 05:44

I\'m starting objective-c development and I would like to ask the best way to implement a list of keys and values.

In Delphi there is the class TDict

3条回答
  •  借酒劲吻你
    2021-02-07 06:19

    You'd want to implement your example along these lines:

    EDIT:

    //NSMutableDictionary myDictionary = [[NSMutableDictionary alloc] init];
    NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];
    
    NSNumber *value = [myDictionary objectForKey:myWord];
    
    if (value)
    {
        NSNumber *nextValue = [NSNumber numberWithInt:[value intValue] + 1];
        [myDictionary setObject:nextValue  forKey:myWord];
    } 
    else
    {
        [myDictionary setObject:[NSNumber numberWithInt:1] forKey:myWord]
    }
    

    (Note: you can't store ints or other primitives directly in a NSMutableDictionary, hence the need to wrap them in an NSNumber object, and make sure you call [myDictionary release] when you've finished with the dictionary).

提交回复
热议问题