我试图将十六进制值作为无符号整数传递给使用动态链接的方法。我作为参数传递的值以某种方式被破坏了。怎么了?
- (void)callPerformSelector
{
NSNumber *argument = [NSNumber numberWithUnsignedInt:(unsigned int)0xFFFFFFFF];
SEL selector = NSSelectorFromString(@"testPerformSelector:");
NSLog(@"testPerformSelector object %@", argument);
[self performSelector:selector withObject:argument];
}
- (void)testPerformSelector:(unsigned int) arg1
{
NSLog(@"testPerformSelector unsigned int %u", arg1);
NSLog(@"testPerformSelector hex %X", arg1);
}
输出是:
testPerformSelector object 4294967295
testPerformSelector unsigned int 4294967283
testPerformSelector hex FFFFFFF3
最佳答案
因为它应该是:
- (void)callPerformSelector
{
NSNumber *argument = @0xFFFFFFFF;
SEL selector = @selector(testPerformSelector:);
NSLog(@"testPerformSelector object %@", argument);
[self performSelector:selector withObject:argument];
}
- (void)testPerformSelector:(NSNumber *) arg1
{
NSLog(@"testPerformSelector unsigned int %u", arg1.unsignedIntValue);
}
unsigned int
和 NSNumber *
是两个不同的东西关于objective-c - 为什么将 unsigned int 传递给 performSelector 会丢失位?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40508865/