几个备选方案。
继续使用字符串作为键,但速度更快:
使用字符串比你真正需要的要重一些,但它至少很简单。
使用-[NSString stringByAppendingString]
会更快。解析格式字符串需要做很多工作。
return [[NSStringFromClass(clazz) stringByAppendingString:@"_"] stringByAppendingString:NSStringFromSelector(selector)];
使用单个NSMutableString
而不是制作中间字符串可能会更好。配置文件并查看。
NSMutableString* result = [NSStringFromClass(clazz) mutableCopy];
[result appendString:@"_"];
[result appendString:NSStringFromSelector(selector)];
return result;
使用自定义对象作为键:
您可以将自定义对象作为引用类和选择器的键。在它上面实现NSCopying
and -isEqual:
,-hash
所以你可以将它用作字典中的键。
@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];
}
消除中间人:
如果您永远不需要将类和选择器从关键对象中拉出,那么您可以使用上面计算的哈希值,存储在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];
}