Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As <code>NSObject</code> provides <code>isEqual:</code>, and all your objects are descendants of <code>NSObject</code>, then the the simple answer is that a default implementation is provided.</p> <p>Now you are concerned over the algorithm this default uses, and in a comment write <em>"I wouldn't be sure simply by testing"</em>. Let's look at testing, just for fun ;-)</p> <p>Now <code>isEqual:</code> is a rather fundamental method, if Apple decided to change its semantics the consequences could be significant and not good. So while Apple is free to change how it is implemented provided the <em>semantics</em> remain the same, which means the same objects compare equal after the change as before. Now you've mentioned three possible algorithms <code>isEqual:</code> could use:</p> <ol> <li>Pointer comparison - is it the <em>exact same</em> object</li> <li>Shallow comparison - do the fields of the object have the same value compared directly</li> <li>Deep comparison - do the non-pointer-valued fields compared directly have the same value, and do the pointer-valued fields compare equal using <code>isEqual:</code></li> </ol> <p>These all have different semantics, whichever one Apple has chosen it can't change without breaking a lot of code. And different semantics means you can test...</p> <p>Coding as I type, errors expected! Only important bits included:</p> <pre><code>@implementation A - (BOOL) isEqual:(id)other { NSLog(@"A.isEqual called"); return self == other; // true iff same object } @end @interface B @property (readwrite) int anInteger; @property (readwrite) A *anA; @end @implementation B @synthesize anInteger, anA; @end // Let's test the algorithm A *myA = [A new]; B *bOne = [B new]; B *bTwo = [B new]; bOne.anInteger = 42; bOne.anA = myA; bTwo.anInteger = 42; bTwo.anA = myA; // What output is produced (all of it!) NSLog(@"[bOne isEqual:bTwo] -&gt; %@", [bOne isEqual:bTwo] ? @"Yes" : @"No"); </code></pre> <p>HTH a little.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload