【问题标题】:Different results of NSNumber to BOOL between iOS simulator and device in iOS8iOS8中iOS模拟器和设备之间NSNumber到BOOL的不同结果
【发布时间】:2015-03-06 03:02:23
【问题描述】:

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

我找不到导致不同结果的根本原因。

有人可以帮忙吗?谢谢

【问题讨论】:

标签: ios objective-c ios8 boolean nsnumber


【解决方案1】:

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
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多