Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a string in a NSDictionary? [duplicate]

Possible Duplicate:
How to add to an NSDictionary

When I do that :

NSDictionary *dic = [NSDictionary dictionary];
[dic setValue:@"nvjd" forKey:@"name"];

my app just crash. I don't understand why. How should I add a string in a dictionary ?

like image 431
Alexis Avatar asked Nov 30 '22 14:11

Alexis


1 Answers

You need to make your dictionary mutable, otherwise it would not respond to the setValue:forKey: selector:

NSMutableDictionary *dic = [NSMutableDictionary dictionary];

This is a common pattern in cocoa: you often see classes declared in pairs: NSArray/NSMutableArray, NSSet/NSMutableSet, NSString/NSMutableString, and so on. Mutable version is capable of doing everything that the immutable version can do, but it also supports operations that change its content.

like image 147
Sergey Kalinichenko Avatar answered Dec 21 '22 01:12

Sergey Kalinichenko