문제

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:?

도움이 되었습니까?

해결책

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

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top