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 6609227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:41:35+00:00 2026-05-25T19:41:35+00:00

I’ve always used NSDictionaries with strings as keys, and pretty much all the examples

  • 0

I’ve always used NSDictionaries with strings as keys, and pretty much all the examples on the web/books/etc. are the same. I figured that I’d try it with a custom object for a key. I’ve read up on implementing the “copyWithZone” method and created the following basic class:

@interface CustomClass : NSObject
{
    NSString *constString;
}

@property (nonatomic, strong, readonly) NSString *constString;

- (id)copyWithZone:(NSZone *)zone; 

@end

@implementation CustomClass

@synthesize constString;

- (id)init
{
    self = [super init];
    if (self) {
        constString = @"THIS IS A STRING";
    }
    return self;
}

- (id)copyWithZone:(NSZone *)zone
{
    CustomClass *copy = [[[self class] allocWithZone: zone] init];
    return copy;
}

@end

Now I’m trying to just add one of these objects with a simple string value, and then getting the string value back out to log to the console:

CustomClass *newObject = [[CustomClass alloc] init];
NSString *valueString = @"Test string";
NSMutableDictionary *dict =
[[NSMutableDictionary alloc] initWithObjectsAndKeys:valueString, newObject, nil];

    NSLog(@"Value in Dictionary: %@", [dict objectForKey: newObject]);
    // Should output "Value in Dictionary: Test string"

Unfortunately the log displays a (null). I’m pretty sure I’m missing something really obvious, and feel like I need another set of eyes.

  • 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-25T19:41:36+00:00Added an answer on May 25, 2026 at 7:41 pm

    NSDictionary key objects work off three methods:

    • -(NSUInteger)hash
    • -(BOOL)isEqual:(id)other
    • -(id)copyWithZone:(NSZone*)zone

    The default NSObject implementation of hash and isEqual: only use the object’s pointer, so when your object is copied via copyWithZone: the copy and the original object are no longer equal.

    What you need is something like this:

    @implementation CustomClass
    
    -(NSUInteger) hash;
    {
        return [constString hash];
    }
    
    -(BOOL) isEqual:(id)other;
    {
        if([other isKindOfClass:[CustomClass class]])
            return [constString isEqualToString:((CustomClass*)other)->constString];
        else
            return NO;
    }
    - (id)copyWithZone:(NSZone *)zone
    {
        CustomClass *copy = [[[self class] allocWithZone: zone] init];
        copy->constString = constString; //might want to copy or retain here, just incase the string isn't a constant
        return copy;
    }
    
    @end
    

    It’s a little bit difficult to find this out from the documentation. The overview for NSDictionary tells you about isEqual: and NSCopying:

    Within a dictionary, the keys are unique. That is, no two keys in a
    single dictionary are equal (as determined by isEqual:). In general, a
    key can be any object (provided that it conforms to the NSCopying
    protocol—see below), but note that when using key-value coding the key
    must be a string (see “Key-Value Coding Fundamentals”).

    And if you have a look at the documentation for -[NSObject isEqual:] it tells you about hash:

    If two objects are equal, they must have the same hash value. This
    last point is particularly important if you define isEqual: in a
    subclass and intend to put instances of that subclass into a
    collection. Make sure you also define hash in your subclass.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.