Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A few alternatives.</p> <h2>Keep using a string as a key, but do it faster:</h2> <p>Using a string is a bit more heavyweight than you really need, but it is at least simple. </p> <p>Using <code>-[NSString stringByAppendingString]</code> would be faster. Parsing format strings is a lot of work.</p> <pre><code>return [[NSStringFromClass(clazz) stringByAppendingString:@"_"] stringByAppendingString:NSStringFromSelector(selector)]; </code></pre> <p>It may be better to use a single <code>NSMutableString</code> instead of making intermediate strings. Profile it and see.</p> <pre><code>NSMutableString* result = [NSStringFromClass(clazz) mutableCopy]; [result appendString:@"_"]; [result appendString:NSStringFromSelector(selector)]; return result; </code></pre> <h2>Use a custom object as a key:</h2> <p>You can make a custom object as the key that refers to the class and selector. Implement <code>NSCopying</code> and <code>-isEqual:</code> and <code>-hash</code> on it, so you can use it as a key in a dictionary.</p> <pre><code>@interface MyKey : NSObject &lt;NSCopying&gt; { 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-&gt;_clazz &amp;&amp; _selector == otherKey-&gt;_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) &lt;&lt; howmuch) | (((NSUInteger)val) &gt;&gt; (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]; } </code></pre> <h2>Eliminate the middleman:</h2> <p>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 <code>NSNumber</code>.</p> <pre><code>// 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) &lt;&lt; howmuch) | (((NSUInteger)val) &gt;&gt; (NSUINT_BIT - howmuch))) + (NSNumber*)keyForClass:(Class)clazz andSelector:(SEL)selector { NSUInteger hash = NSUINTROTATE([clazz hash], NSUINT_BIT / 2) ^ (NSUInteger)selector; return [NSNumber numberWithUnsignedInteger:hash]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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