How is comparing operator '==
' for NSObject
?
Method -isEqual:
works fine for me, but when I'm using -isEqual
I need to check if objects exists. With '==
' I don't need to check this but where I can find documentation for it?

- 1,540
- 1
- 16
- 27
-
Do you need to compare `NSObject` or check its existence? – Akhilrajtr Mar 28 '14 at 12:10
-
What do you mean by "but with it I need to check if objects exists"? – Monolo Mar 28 '14 at 12:13
-
`==`, when used with pointers, checks whether the *pointers* are equal. You could have two NSStrings that both said "ABC", but would compare not-equal because they were *different* objects with *different* addresses. – Hot Licks Mar 28 '14 at 12:16
-
@Monolo I mean when I'm comparing with -isEqual method, I need to check if objects are null – Igor Bidiniuc Mar 28 '14 at 12:17
-
(And, frankly, if you don't understand this sort of thing you're going to be lost programming in Objective-C.) – Hot Licks Mar 28 '14 at 12:17
-
@HotLicks, thanks for advice. Please compile this: NSString *anA = @"a"; NSString *anotherA = @"a"; if (anA == anotherA) { NSLog(@"Hello Hot Licks"); } – Igor Bidiniuc Mar 28 '14 at 12:27
-
Those will compare equal with `==` because they're both pointers to the exact same object. If you had, on the other hand, gotten one of your strings by substringing another string, they would not compare equal with `==`. – Hot Licks Mar 28 '14 at 15:51
2 Answers
The == operator tests whether the two expressions are the same pointer to the same object. Cocoa calls this relation “identical”
To test whether two objects are equal, you would send one of them an isEqual:

- 3,673
- 2
- 19
- 36
-
This I know, but why 2 different NSString objects can be equal with == operator? This was question. – Igor Bidiniuc Mar 28 '14 at 13:16
-
Just because of : NSString *stringA=@"abc"; NSString *stringB=stringA; if (stringA==stringB) {NSLog(@"==");} – Armand DOHM Mar 28 '14 at 13:47
-
While I'm sure @ArmandDOHM's answer is what the question wanted to know, it might be worth noting that in the example given stringA and stringB both point to the self same NSString object. Two different NSString objects CANNOT be equal with '==' operator. – George Green Mar 28 '14 at 13:59
-
1
-
From Apple documentation:
Returns a Boolean value that indicates whether the receiver and a given object are equal. (required) This method defines what it means for instances to be equal. For example, a container object might define two containers as equal if their corresponding objects all respond YES to an isEqual: request. See the NSData, NSDictionary, NSArray, and NSString class specifications for examples of the use of this method. If two objects are equal, they must have the same hash value. This last point is particularly important if you define isEqual: in a subclass and intend to put instances of that subclass into a collection. Make sure you also define hash in your subclass.
if you do like this
if([obj1 isEqual:obj2])
and obj1
, or obj2
is nil
then you will get NO
. (if this is what you meant by your question)
- Now
if(obj1 == obj2)
This is a pointer comparison. Pointers

- 4,908
- 24
- 36
-
1thanks, I didn't understand this, why when I'm comparing 2 NSString-s or NSArray-s return YES. This explain why. – Igor Bidiniuc Mar 28 '14 at 12:53