I want to add items to mutable array from a dictionary. Problem is I want to check existing array items before adding new item. If same item is already there in the array, I want to replace it. else add the new item. How could I do it?
3 Answers
You could perhaps use an NSMutableSet rather than an NSMutableArray. The addObject
method on NSMutableSet will only "add a given object to the set, if it is not already a member."
If you'd like to check membership before adding to the set anyway, you can check the result of:
[mySet containsObject:myObjectFromDictionary]
...which returns a simple BOOL value indicating whether the set already contains an object whose isEqual
method returns true when your object is passed to it.
(For a little extra functionality, NSCountedSet will keep track of the number of objects added to the "set" for which isEqual:
returns true)

- 1,190
- 12
- 15
-
this will work if the new object does not need to replace the old item; NSMutableSet gives the original object precedence within the array, while it sounds like the OP wants the new object to have precedence--but perhaps it would be good for the OP to consider whether replacing the old is strictly necessary. – matthias Sep 14 '11 at 15:50
-
If the new object and the original object respond `YES` to `isEqual:` when they are, in fact, not equal, then the OP has far, far, more serious problems than just this question.... – bbum Sep 14 '11 at 15:52
You could compare the result of : [yourArray indexOfObject:yourObject];
against NSNotFound
to know if the object is in the array.
It will give you the index of the object to replace, or if it is equal to NSNotFound
, you will add it.
Objects equality is tested with isEqual:
method.
NSArray class reference.
On the face of it, both Vincent's and Rich's answers are correct.
However, there is a conceptual issue in the original question that hasn't been addressed.
Namely, that "membership in an array" via indexOfObject:
(or containsObject:
in a set) is ultimately done by comparing the two objects using isEqual:
.
If isEqual:
returns YES, then the two objects better had damned well be functionally identical in your code or else you have other, significantly more serious, problems in your design and implementation.
Thus, the real question should be "How do I detect if an object is already in an array and not add it?" and Rich's and Vincent's answer are both still correct.
I.e. you should only need to check for presence and, if present, take no action.
(Note that there are esoteric situations where replacement is actually warranted, but they are both truly esoteric and not generally used within the context of a mutable collection)

- 162,346
- 23
- 271
- 359
-
1Agreed. An awful lot of functionality can rest on the correct implementation of `isEqual`. There's a great SO post on the matter [here](http://stackoverflow.com/questions/1112373/implementing-hash-isequal-isequalto-for-objective-c-collections). (Also, just a brief note to say that NSArray also supports the `containsObject` method which, as with NSSet, returns a BOOL.) – Rich Pollock Sep 14 '11 at 16:06