Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

iphone - How to compare if two objects are really the same object?

I want to compare if an variable A represents the same object as variable B does.

Could I do that with the == operator?

Or what else is this exactly looking at? I think I need to check for the memory adress of the object where the variable is pointing to, right?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The == operator tests whether the two expressions are the same pointer to the same object. Cocoa calls this relation “identical” (see, for example, NSArray's indexOfObjectIdenticalTo:).

To test whether two objects are equal, you would send one of them an isEqual: message (or a more specific message, such as isEqualToString:, if it responds to one), passing the other object. This would return YES if you really only have one object (equal to itself, obviously) or if you have two objects that are equal. In the latter case, == will evaluate to NO.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...