Lucas Vallim da Costa
Lucas Vallim da Costa

Reputation: 203

iOS Add object in key on NSMutableDictionary (not so simple as that)

How do I add more then one object for the same key? is that possible?

For example, I need to have a dictionary like this:

object:textField.text for key @"Permanent key"

I need to always add objects to that permanent key depending on how many times the user types something on the textfield.

So my dictionary would have lots of entries for the key "permanent key".

Upvotes: 0

Views: 1340

Answers (3)

Nicolas Miari
Nicolas Miari

Reputation: 16256

If you were able to add (say) object A and object B for the same key; Which object would you get when you retrieve with that key? A or B?

Keys must be unique by definition; They are like the numerical indices of an array, but instead of consecutive numbers, they can be arbitrary strings.

As others pointed out, you want to store an array of objects for that key, instead of a single object.

Upvotes: 0

8vius
8vius

Reputation: 5836

This is not possible, keys have to be unique for each value in the dictionary.

If what you need is several objects like this, why not just make an NSMutableArray or NSMutableSet of them? Or have your object be itself an NSMutableArray and you assign it to that key and simply add to this NSMutableArray the new values that are required.

Upvotes: 5

vikingosegundo
vikingosegundo

Reputation: 52237

You can Use an NSMutableArray as object, in which you put the objects you want to assign to that unique key.

Upvotes: 3

Related Questions