Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Like you said, you can create a Pair class:</p> <pre><code>//Pair.h @interface Pair : NSOpject &lt;NSCopying&gt; { id&lt;NSCopying&gt; left; id&lt;NSCopying&gt; right; } @property (nonatomic, readonly) id&lt;NSCopying&gt; left; @property (nonatomic, readonly) id&lt;NSCopying&gt; right; + (id) pairWithLeft:(id&lt;NSCopying&gt;)l right:(id&lt;NSCopying&gt;)r; - (id) initWithLeft:(id&lt;NSCopying&gt;)l right:(id&lt;NSCopying&gt;)r; @end //Pair.m #import "Pair.h" @implementation Pair @synthesize left, right; + (id) pairWithLeft:(id&lt;NSCopying&gt;)l right:(id&lt;NSCopying&gt;)r { return [[[[self class] alloc] initWithLeft:l right:r] autorelease]; } - (id) initWithLeft:(id&lt;NSCopying&gt;)l right:(id&lt;NSCopying&gt;)r { if (self = [super init]) { left = [l copy]; right = [r copy]; } return self; } - (void) finalize { [left release], left = nil; [right release], right = nil; [super finalize]; } - (void) dealloc { [left release], left = nil; [right release], right = nil; [super dealloc]; } - (id) copyWithZone:(NSZone *)zone { Pair * copy = [[[self class] alloc] initWithLeft:[self left] right:[self right]]; return copy; } - (BOOL) isEqual:(id)other { if ([other isKindOfClass:[Pair class]] == NO) { return NO; } return ([[self left] isEqual:[other left]] &amp;&amp; [[self right] isEqual:[other right]]); } - (NSUInteger) hash { //perhaps not "hashish" enough, but probably good enough return [[self left] hash] + [[self right] hash]; } @end </code></pre> <p>Edit:</p> <p>Some notes on creating objects that can be keys:</p> <ol> <li>They have to conform to the <code>NSCopying</code> protocol, since we don't want the key to change out from underneath us.</li> <li>Since the Pair itself is copied, I make sure the objects in the pair are also copied.</li> <li>Keys have to implement <code>isEqual:</code>. I implement the <code>hash</code> method for good measure, but it's probably not necessary.</li> </ol>
 

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