user141302
user141302

Reputation:

avoiding adding same key in NSMutableDictionary in iphone sdk?

i add xml data in NSMutableDictionary through element name as key.How can I check that the key is already available in dictionary when i add the same key again without loop?

Upvotes: 1

Views: 526

Answers (2)

Jakob W
Jakob W

Reputation: 3377

If you ask the dictionary for the object with that key it will return nil if it is not found.

if([yourDictionary objectForKey:@"yourKey"] == nil){
  //Do stuff when yourKey is not in the dictionary yet
}

Or, if you don't care what happens to the previous value I think you could use

[yourDictionary setObject:someObject forKey:someKey]

instead of adding objects to the dictionary.

Upvotes: 1

banu
banu

Reputation: 787

try this:

([myDictionary objectForKey:@"my key"] == nil)

objectForKey will return nil if a key doesn't exists.

Upvotes: 1

Related Questions