From what I have read, keys in dictionaries are unique.
Consider this code:
NSMutableDictionary *mydic = [NSMutableDictionary dictionary];
[mydic setObject:@"value1" forKey:@"key1"];
[mydic setObject:@"value1" forKey:@"key1"];
[mydic setObject:@"value1" forKey:@"key1"];
Why can I run this without any error? What should I do to avoid duplicate keys?
Yes keys are unique. Calling
-setObject:forKey:with an existing key overrides the old value — it sets values, not adds values. You can check that:If you don’t want existing items to be overridden, check if it exists with
-objectForKey::