Reputation: 1694
i have a nsmutabeldictionary
like this
{
alert = "test from dot net";
badge = 1;
sound = default;
}
and want to a key 'ID' with value 10 just like
{
alert = "test from dot net";
badge = 1;
sound = default;
ID = 10;
}
please help
Upvotes: 0
Views: 433
Reputation: 32066
From your comment:
'NSUnknownKeyException', reason: '[<__NSDictionaryI 0x1ede8740> setValue:forUndefinedKey:]:
It looks as if you actually have an immutable dictionary (note the I suffix).
I imagine your code looks like this:
//Even though you are assigning to a mutable object
//The method may not return an mutable dictionary
NSMutableDictionary *d = [someObject getDictionary];
You should instead use:
NSMutableDictionary *d = [NSMutableDictionary dictionaryWithDictionary:[someObject getDictionary]];
Then you can comfortably use:
[d setObject:@10 forKey:@"ID"];
Upvotes: 1
Reputation: 955
To add new object with new Key to Dictionary, you can use SetObject:forKey Method.
Upvotes: 0
Reputation: 21147
You just have to call the setObject:forKey
Adds a given key-value pair to the dictionary.
- (void)setObject:(id)anObject forKey:(id < NSCopying >)aKey
Parameters
anObject
The value for aKey. A strong reference to the object is maintained by the dictionary. Raises an NSInvalidArgumentException if anObject is nil. If you need to represent a nil value in the dictionary, use NSNull.
aKey
The key for value. The key is copied (using copyWithZone:; keys must conform to the NSCopying protocol). Raises an NSInvalidArgumentException if aKey is nil. If aKey already exists in the dictionary anObject takes its place.
In your example you can call it like this:
[yourMutableDictionary setObject:[NSNumberWithInt:10] forKey:@"ID"]
also, remember that you have to pass an NSObject. So you should use [NSNumberWithInt:10]
instead of just 10
.
Upvotes: 1