Two related questions:
-
When you use
[NSSet setWithArray:], does it remove duplicate object for you automatically? -
How can you tell
NSSetexactly what you want “duplicate” to mean? I.e. if you have a bunch of “College course” objects, each with a name and section number, and you wanted to transfer to an NSSet, keeping only one of each college course for a given name (for example, if you had three sections of Calculus, how would you tell it to only keep one section of calculus, even if their section numbers are different, so they’re not perceived as identical by default).
Thanks! Let me know if that question was unclear at all. I was having trouble figuring out a way to word it.
Edit: This question is specific to NSManagedObjects, whose isEqual: method cannot be overridden.
From the documentation:
Equality is determined here as throughout Cocoa with the
-isEqual:method (and the-hashmethod). If you want two custom objects to be considered equal, you should override these appropriately, and you must override both. These are generally used so that objects that really are equivalent and generally interchangeable (but are separate objects) can be seen as such. In your example, it sounds like the college course objects really are “different” (ie, they represent different classes, even if they might share the same overall “calculus” topic), so it seems problematic to call those object instances “equal” if this is a large scale project/code base. In your case, you might consider adding the object to the set one by one and do your own comparisons as you add to make sure you get one of each “topic”.