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