Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add values in NSMutableDictionary in iOS with Objective-C

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 TDictionary and I use it like this:

myDictionary : TDictionary<string, Integer>;

bool found = myDictionary.TryGetValue(myWord, currentValue);
if (found)
{
    myDictionary.AddOrSetValue(myWord, currentValue+1);
} 
else
{
    myDictionary.Add(myWord,1);
}

How can I do it in objective-c? Is there equivalent functions to the above mentioned AddOrSetValue() or TryGetValue()?

Thank you.

like image 649
Miguel E Avatar asked Jun 29 '11 11:06

Miguel E


People also ask

How do you add value in NSMutableDictionary?

You can simply say: myDictionary[myWord] = nextValue; Similarly, to get a value, you can use myDictionary[key] to get the value (or nil).

How do I create an NSArray in Objective C?

Creating NSArray Objects Using Array Literals In addition to the provided initializers, such as initWithObjects: , you can create an NSArray object using an array literal. In Objective-C, the compiler generates code that makes an underlying call to the init(objects:count:) method.

What is NSMutableDictionary in Swift?

The NSMutableDictionary class declares the programmatic interface to objects that manage mutable associations of keys and values. It adds modification operations to the basic operations it inherits from NSDictionary . NSMutableDictionary is “toll-free bridged” with its Core Foundation counterpart, CFMutableDictionary .

What is NSDictionary?

An object representing a static collection of key-value pairs, for use instead of a Dictionary constant in cases that require reference semantics.


Video Answer


3 Answers

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).

like image 103
Tom Jefferys Avatar answered Nov 15 '22 13:11

Tom Jefferys


The other answers are correct, but there is more modern syntax for this now. Rather than:

[myDictionary setObject:nextValue  forKey:myWord];

You can simply say:

myDictionary[myWord] = nextValue;

Similarly, to get a value, you can use myDictionary[key] to get the value (or nil).

like image 33
Jesse Rusak Avatar answered Nov 15 '22 14:11

Jesse Rusak


Yep:

- (id)objectForKey:(id)key;
- (void)setObject:(id)object forKey:(id)key;

setObject:forKey: overwrites any existing object with the same key; objectForKey: returns nil if the object doesn't exist.

Edit:

Example:

- (void)doStuff {
  NSMutableDictionary *dict = [NSMutableDictionary dictionary];

  [dict setObject:@"Foo" forKey:@"Key_1"]; // adds @"Foo"
  [dict setObject:@"Bar" forKey:@"Key_2"]; // adds @"Bar"

  [dict setObject:@"Qux" forKey:@"Key_2"]; // overwrites @"Bar"!

  NSString *aString = [dict objectForKey:@"Key_1"]; // @"Foo"
  NSString *anotherString = [dict objectForKey:@"Key_2"]; // @"Qux"
  NSString *yas = [dict objectForKey:@"Key_3"]; // nil
}

Reedit: For the specific example there exists a more compact approach:

[dict
  setObject:
    [NSNumber numberWithInteger:([[dict objectForKey:@"key"] integerValue] + 1)]
  forKey:
    @"key"
 ];

Crazy indentation for readability.

like image 43
Williham Totland Avatar answered Nov 15 '22 14:11

Williham Totland