objective-ccore-datansset

Which is better, NSSet’s containsObject or fast enum?


I need to determine whether an object is included in a Core Data to-many relationship (which is an NSSet), and I’m trying to decide which of two solutions is better:

Solution 1)

if ([managedObject.items containsObject:itemOfInterest])
    return …

Solution 2)

for (NSManagedObject *item in managedObject.items)
    if ([item == itemOfInterest])
        return …

Solution 1 is more concise, but the NSSet Class Ref says fast enumeration performs better than NSSet’s objectEnumerator. Does it also perform better than containsObject?


Solution

  • I'd always go for option 1.

    Its more concise, I can tell exactly what your trying to do with the code and chances are that the containsObject contains some pretty nifty optimisations.