I have an NSArray storing NSDictionaries. Each dictionary has two keys; a name and a sub-array containing ‘items’.
I want to check if one of these dictionaries still remains in the NSArray after a certain operation. Before the operation, I store a pointer called ‘orginalDictionary’ to the dictionary I am interested in. I then perform my operation (which removes one of the ‘items’ from the sub-array inside that dictionary). After the operation, I thought I could check if the Dictionary itself (not the removed item) still exists, using
if ([arrayOfDictionaries containsObject:originalDictionary]) {...
but it’s not working as anticipated. Even though the Array still contains the original dictionary (albeit with a modified sub-array inside it), the call returns that the array no longer contains that dictionary.
Can that be right? If an item in an array changes, does containsObject no longer recognize it as the same object? Or is something else going on here?
EDIT
I’m starting to think that the way in which my array is populated is the root cause. The array of dictionaries is created from one or more other arrays, depending on some settings. These arrays are themselves populated using a fetch request, which created the dictionaries based on the fetched objects. So, when there is a change to some data (or perhaps even if the fetch is fired simply by requesting the return result of the array), this is going to be rebuild the returned array, and the dictionaries inside will be new items. Does this sound about right? If so, I suppose I have no option but to store a self-managed unique key?
containsObject:determines whether an object representing the same value is in the array, this is the same test thatisEqual:andindexOfObject:use. E.g. two completely different dictionaries which contain the same key/value pairs will compare equal as they both represent the same value – just as two integer variables compare equal if they contain the same integer value.The method
indexOfObjectIdenticalTo:looks for whether the object is present in the array.However that doesn’t directly solve your problem. If you actually have an
NSMutableArraywhich containsNSMutableDictionarys and you keep a reference to theNSMutableDictionaryyou’re checking for then value or object comparison should work…Either use the debugger to track the actual references (addresses/pointers), or dump them to the console using
NSLog()and the%pformat (e.g.NSLog(@"Tracked dict: %p", originalDictionary)).