Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 422845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:07:18+00:00 2026-05-12T19:07:18+00:00

I have a situation where I want to map a pair of objects to

  • 0

I have a situation where I want to map a pair of objects to a dictionary of information. I’d like to avoid creating an NSDictionary of NSDictionaries of NSDictionaries since that’s simply confusing and difficult to maintain.

For instance if I have two classes, Foo and Bar, I want {Foo, Bar} -> {NSDictionary}

Are there other options than just making a custom class (just to be used as the dictionary key) based on the two types in question? Maybe something akin to STL’s pair type that I just don’t know about?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T19:07:18+00:00Added an answer on May 12, 2026 at 7:07 pm

    Like you said, you can create a Pair class:

    //Pair.h
    @interface Pair : NSOpject <NSCopying> {
      id<NSCopying> left;
      id<NSCopying> right;
    }
    @property (nonatomic, readonly) id<NSCopying> left;
    @property (nonatomic, readonly) id<NSCopying> right;
    + (id) pairWithLeft:(id<NSCopying>)l right:(id<NSCopying>)r;
    - (id) initWithLeft:(id<NSCopying>)l right:(id<NSCopying>)r;
    @end
    
    //Pair.m
    #import "Pair.h"
    @implementation Pair
    @synthesize left, right;
    
    + (id) pairWithLeft:(id<NSCopying>)l right:(id<NSCopying>)r {
     return [[[[self class] alloc] initWithLeft:l right:r] autorelease];
    }
    
    - (id) initWithLeft:(id<NSCopying>)l right:(id<NSCopying>)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]] && [[self right] isEqual:[other right]]);
    }
    
    - (NSUInteger) hash {
      //perhaps not "hashish" enough, but probably good enough
      return [[self left] hash] + [[self right] hash];
    }
    
    @end
    

    Edit:

    Some notes on creating objects that can be keys:

    1. They have to conform to the NSCopying protocol, since we don’t want the key to change out from underneath us.
    2. Since the Pair itself is copied, I make sure the objects in the pair are also copied.
    3. Keys have to implement isEqual:. I implement the hash method for good measure, but it’s probably not necessary.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a situation where i want to add LinePragmas to CodeDom objects. But
I have this situation where I want to display a list of Administration objects
Using RhinoMocks, I have a catch-22 situation: I want to verify that a method
I am in a situation where I want to have a map where the
I have a situation where I want a bash script to replace an entire
I have a situation where I want certain code to be executed no matter
I have a situation where i want to return List<> from this function public
I have a situation where I want a Java client to have a two-way
I have a situation where I want to create a mapping from a tuple
I have a situation where I want my program to read in some numbers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.