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

  • SEARCH
  • Home
  • 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 646381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:36:20+00:00 2026-05-13T21:36:20+00:00

All objects used as keys in NS(Mutable)Dictionaries must support the NSCopying protocol, and those

  • 0

All objects used as keys in NS(Mutable)Dictionaries must support the NSCopying protocol, and those objects are copied when they’re used in the dictionary.

I frequently want to use heavier weight objects as keys, simply to map one object to another. What I really mean when I do that is effectively:

[dictionary setObject:someObject forKey:[NSValue valueWithPointer:keyObject]];

(“When I come back and hand you this same key object instance again, get me that same value out.”)

…which is exactly what I end up doing to get around this design sometimes. (Yes, I know about NSMapTable in desktop Cocoa; but e.g. iPhone doesn’t support this.)

But what I don’t really get is why copying the key is necessary or desirable in the first place. What does it buy the implementation or caller?

  • 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-13T21:36:20+00:00Added an answer on May 13, 2026 at 9:36 pm

    The copy ensures that the values used as keys don’t change “underhand” while being used as keys. Consider the example of a mutable string:

    NSMutableString* key = ... 
    NSMutableDictionary* dict = [[NSMutableDictionary alloc] init];
    
    [dict setObject: ... forKey: key];
    

    Let’s assume that the dictionary did not copy the key, but instead just retained it. If now, at some later point, the original string is modified, then it is very likely that you are not going to find your stored value in the dictionary again even if you use the very same key object (i.e., the one key points to in the example above).

    In order to protect yourself against such a mistake, the dictionary copies all keys.

    Note, by the way, that it is simple enough to define -copyWithZone: as just doing return [self retain]. This is allowed and good code if your object is immutable, and the NSCopying contract is specifically designed such that the object returned has to be (sorta, kinda) immutable:

    Implement NSCopying by retaining the original instead of creating a new copy when the class and its contents are immutable.

    (from NSCopying Reference)

    and

    The copy returned is immutable if the consideration “immutable vs. mutable” applies to the receiving object; otherwise the exact nature of the copy is determined by the class.

    (from -copyWithZone: Reference)

    Even if your objects are not immutable, you might get away with that implementation if you only ever use identity-based equality/hash implementations, i.e., implementations which are not affected in any way by the object’s internal state.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to get intellisense for all the keys in a Dictionary<string,string>
I want to retrieve all objects (not DOM elements) of a given type created
Is defining width for all objects (inputs, divs etc.) with percentage is a good
I need to perform an operation on all objects in my NSOutlineView when I
I want to achieve something relatively simple: I want to retrieve all objects from
Is there a way to look through the cache for all objects in the
I would like to get a reference to all objects in the Java heap,
Is it possible to create a criteria that lists all objects sorted by sope
Is it possible to call a class method of all objects in a list?
I have a vector for storing the pointers of all objects (created by new

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.