Frage

I have 2 Core Data objects of the same type. I got them via different ways.

At the same time these objects have an NSString field (identifier).

So what is better?

1)compare objects via isEqual:

2)compare objects via == (is it possible in general?)

3)compare objects' fields via isEqualToString:?

War es hilfreich?

Lösung

Basically, never use == on objects. Always use isEqual:.

If you're using a predicate then you can use LIKE on the identifier.

Andere Tipps

If you are trying to determine whether or not two objects are the semantically equal and you can describe what equality looks like for that type, then use isEqual. For example, if you have a Person class, you might define equality based on SSN (in the US) or something equally unique.

If you are trying to determine that two objects have something specific in common, but it doesn't necessarily imply that they are equal (for example, using the same Person example, if you want find out if two instances have the same last name), compare the attributes using isEqualToString:. It would not make sense to put this comparison into isEqual for the type.

If you are trying to find out if you have two NSManagedObject instances from your persistent store that are actually the same object fetched via different NSManagedObjectContexts, use the objectID for comparison.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top