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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:52:09+00:00 2026-06-17T12:52:09+00:00

I have a dictionary that stores an object using a combination of the class

  • 0

I have a dictionary that stores an object using a combination of the class name and selector as the key. I’m using the following function in order to calculate the hash:

+(NSString*) getKeyForClass:(Class) clazz andSelector:(SEL) selector {
    return [NSString stringWithFormat:@"%@_%@",NSStringFromClass(clazz), NSStringFromSelector(selector)];
}

While running a profiler i’ve discovered that this function is the bottleneck of the computation. Is there a better (= more efficient) way to create a key from a class and a selector?

  • 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-06-17T12:52:11+00:00Added an answer on June 17, 2026 at 12:52 pm

    A few alternatives.

    Keep using a string as a key, but do it faster:

    Using a string is a bit more heavyweight than you really need, but it is at least simple.

    Using -[NSString stringByAppendingString] would be faster. Parsing format strings is a lot of work.

    return [[NSStringFromClass(clazz) stringByAppendingString:@"_"] stringByAppendingString:NSStringFromSelector(selector)];
    

    It may be better to use a single NSMutableString instead of making intermediate strings. Profile it and see.

    NSMutableString* result = [NSStringFromClass(clazz) mutableCopy];
    [result appendString:@"_"];
    [result appendString:NSStringFromSelector(selector)];
    return result;
    

    Use a custom object as a key:

    You can make a custom object as the key that refers to the class and selector. Implement NSCopying and -isEqual: and -hash on it, so you can use it as a key in a dictionary.

    @interface MyKey : NSObject <NSCopying>
    {
        Class _clazz;
        SEL _selector;
    }
    
    - (id)initWithClass:(Class)clazz andSelector:(SEL)selector;
    
    @end
    
    @implementation MyKey
    
    - (id)initWithClass:(Class)clazz andSelector:(SEL)selector
    {
        if ((self = [super init])) {
            _clazz = clazz;
            _selector = selector;
        }
        return self;
    }
    
    - (id)copyWithZone:(NSZone*)zone
    {
        return self; // this object is immutable, so no need to actually copy it
    }
    
    - (BOOL)isEqual:(id)other
    {
        if ([other isKindOfClass:[MyKey class]]) {
            MyKey* otherKey = (MyKey*)other;
            return _clazz == otherKey->_clazz && _selector == otherKey->_selector;
        } else {
            return NO;
        }
    }
    
    // Hash combining method from http://www.mikeash.com/pyblog/friday-qa-2010-06-18-implementing-equality-and-hashing.html
    #define NSUINT_BIT (CHAR_BIT * sizeof(NSUInteger))
    #define NSUINTROTATE(val, howmuch) ((((NSUInteger)val) << howmuch) | (((NSUInteger)val) >> (NSUINT_BIT - howmuch)))
    
    - (NSUInteger)hash
    {
        return NSUINTROTATE([_clazz hash], NSUINT_BIT / 2) ^ (NSUInteger)_selector;
    }
    
    @end
    
    + (MyKey*)keyForClass:(Class)clazz andSelector:(SEL)selector
    {
        return [[MyKey alloc] initWithClass:clazz andSelector:selector];
    }
    

    Eliminate the middleman:

    If you never need to pull the class and selector out of your key object, then you can just use the hash as computed above, stored in an NSNumber.

    // Hash combining method from http://www.mikeash.com/pyblog/friday-qa-2010-06-18-implementing-equality-and-hashing.html
    #define NSUINT_BIT (CHAR_BIT * sizeof(NSUInteger))
    #define NSUINTROTATE(val, howmuch) ((((NSUInteger)val) << howmuch) | (((NSUInteger)val) >> (NSUINT_BIT - howmuch)))
    
    + (NSNumber*)keyForClass:(Class)clazz andSelector:(SEL)selector
    {
        NSUInteger hash = NSUINTROTATE([clazz hash], NSUINT_BIT / 2) ^ (NSUInteger)selector;
        return [NSNumber numberWithUnsignedInteger:hash];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Dictionary object that is formed using a double as its key.
Basically I have been using a Dictionary object in my program that basically took
I have set up a object class that I am using to create my
I have a method that creates a dictionary from NSJSONSerialization class. I then enumerate
I have dictionary that is defined as: Dictionary<string, object> d = new Dictionary<string, object>();
I have a dictionary object with about 60,000 keys that I cache and access
I have data in a python dictionary that I'd like to store in a
If I have, say, 100 items that'll be stored in a dictionary, should I
I have a dictionary that has keys associated with lists. mydict = {'fruits': ['banana',
I have a dictionary that hosts diameter(in mm) and worth of euro coins: CoinsDiameters

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.