当我尝试通过 iOS 模拟器(8.1 和 7.1)运行以下代码时,myMethod 中“going”的值为 YES。
但是当我用我的iPhone(8.1.2)运行时,going的值就变成了NO。
[self performSelector:@selector(myMethod:) withObject:[NSNumber numberWithBool:YES] afterDelay:0.5f];
- (void) myMethod:(BOOL)going {
if (going) {
// do something
}
else {
// do another thing
}
}
我找不到导致不同结果的根本原因。
有人可以帮忙吗?谢谢
最佳答案
myMethod:
采用 BOOL
。不是 NSNumber
。您需要以下内容:
[self performSelector:@selector(myMethod:) withObject:@YES afterDelay:0.5f];
- (void)myMethod:(NSNumber *)going {
if ([going boolValue]) {
// do something
} else {
// do another thing
}
}
另一种选择是使用 dispatch_after
而不是 performSelector:withObject:afterDelay:
。
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self myMethod:YES];
});
- (void)myMethod:(BOOL)going {
if (going) {
// do something
} else {
// do another thing
}
}
关于ios - iOS8 中 iOS 模拟器和设备之间 NSNumber 到 BOOL 的不同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27832654/