Equal vs Identical


#1

I’m trying to understand the difference between these two concepts and I find it useful to try to explain it in my own words – could I just check my understanding please?

Two variables are identical (and unavoidably equal) if they both point to the same address - this is what you’re checking for with the basic NSObject isEqual.

However, two variables can be equal and not identical if they point to two different addresses which happen to have exactly the same content (and presumably the same type?) - and this what the NSString version of isEqual does. Is that right?

But I’m not sure I understand the practical difference between the two NSMutableArray methods. Does it simply mean than indexOfObject can be true if anObject is a separate object sharing the same contents, while indexOfObjectIdentical çan only be true if it’s the object at the same memory address?

Many thanks

David


#2

You’ve nailed it!


#3

Thanks very much for your quick response - sometimes I think I’ve understood something, but when I try to explain it, I find I don’t… so it helps to articulate it.


#4

So very true. Sometimes even forcing yourself to put your question into words (even if you’re talking to a pet or the wall) can help you to suddenly realize the answer.


#5

I’ve found many times I’ve typed out a huge question, and by the time I’m done putting the problem into words I’ve figured it out on my own and then just cancelled the post. Keep up the good work!


#6

Me too – I think half the value of boards like this comes from that very point…


#7

Please correct me, but I understood that the following checks to confirm if the two objects have the same memory address… “are identical… x==y”

But, lets say:

// check to see if strings are Identical
NSString *string1=@“this is a test”;
NSString *string2=@“this is a test”;

//we know that the two strings reside in different memory addresses and therefore doNOT share the same address

if (string1==string2) NSLog(@“This is a true statement”);
else NSLog(@“This is a false statement”);

Result: This is a true statement.

The “==” appears to be evaluating the hash value, not the memory address.

What am I missing?


#8

Actually, the compiler pulled a trick on you and created only one string and pointed both string1 and string2 at it. If you print out the addresses, you’ll find they are the same:


#9

The complier is laughing at me. Thanks Aaron


#10

[quote=“AaronHillegass”]Actually, the compiler pulled a trick on you and created only one string and pointed both string1 and string2 at it. If you print out the addresses, you’ll find they are the same:

How is that possible? Didn’t he create two different string objects? Is this ARC? Or apples way of managing memory since it knows that both objects are “equal” so there is no need to make two copies of them and occupy two memory addresses?


#11

This is correct: The compiler knows that both objects are “equal” and unchangeable so there is no need to make two copies of the same string which would occupy two memory addresses.


#12

Thats amazing! Go apple