What is the best way to properly test object equality against an array of objects?

Posted by radesix on Stack Overflow See other posts from Stack Overflow or by radesix
Published on 2010-03-12T15:24:33Z Indexed on 2010/03/12 15:27 UTC
Read the original article Hit count: 138

Filed under:

My objective is to abort the NSXMLParser when I parse an item that already exists in cache. The basic flow of the program works like this:

1) Program starts and downloads an XML feed. Each item in the feed is represented by a custom object (FeedItem). Each FeedItem gets added to an array.

2) When the parsing is complete the contents of the array (all FeedItem objects) are archived to the disk.

The next time the program is executed or the feed is refreshed by the user I begin parsing again; however, since a cache (array) now exists as each item is parsed I want to see if the object exists in the cache. If it does then I know I have downloaded all the new items and no longer need to continue parsing.

What I am learning, I think, is that I can't use indexOfObject or indexOfObjectIDenticalTo: because these really seem to be checking to see that the objects are using the same memory address (thus identical).

What I want to do is see if the contents of the object are equal (or at least some of the contents). I've done some research and found that I can override the IsEqual method; however, I really don't want to iterate/enumerate through the entire cache contents table for every newly parsed XML FeedItem. Is iterating through the collection and testing each one for equality the only way to do this or is there a better technique I am not aware of?

Currently I am using the following code though I know it needs to change:
NSUInteger index = [self.feedListCache.feedList indexOfObject:self.currentFeedItem]; if (index == NSNotFound) { }

© Stack Overflow or respective owner

Related posts about iphone