我使用为 withObject 方法进行强制转换的适配器方法解决了这个问题。我的问题是我想使用 typedef 枚举并将其作为值传递给 withObject。
我想使用 performSelect 消息调用此方法:
-(void) requestInfosAndPersistByMonsterType:(MonsterTypes)monsterType {
}
如您所见,它请求一个 MonsterTypes typedef 定义如下:
typedef enum
{
MonsterTypeIWerwolf = 0,
MonsterTypeITempler = 1,
MonsterTypeIUndefined,
} MonsterTypes;
实际上,为了能够调用上面的方法,我构建了这个调用它的适配器:
-(void)monsterTypeFromObject:(id)_monsterType {
if ([_monsterType respondsToSelector:@selector(intValue)]) {
int _t = [_monsterType intValue];
switch (_t) {
case MonsterTypeIWerwolf:
_t = MonsterTypeIWerwolf;
break;
case MonsterTypeITempler:
_t = MonsterTypeITempler;
break;
default:
_t = MonsterTypeIUndefined;
break;
}
[self requestInfosAndPersistByMonsterType:_t];
}
}
它是这样使用的:
[self performSelector:@selector(monsterTypeFromObject:) withObject:[NSNumber numberWithUnsignedInt:monsterType] afterDelay:5.0f];
你可以在这里找到更详细的解释:http:
//kerkermeister.net/objective-c-adapter-from-nsinteger-to-id-when-using-performselector-withobject/