繁体   English   中英

iOS模拟器和iOS8中的设备之间NSNumber到BOOL的结果不同

[英]Different results of NSNumber to BOOL between iOS simulator and device in iOS8

当我尝试通过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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM