1

Xcode 的dispatch_after模板

double delayInSeconds = 2.0;
double delayInNanoSeconds = delayInSeconds * NSEC_PER_SEC;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInNanoSeconds);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
});

导致以下警告

隐式转换将浮点数转换为整数:“double”到“int64_t”(又名“long long”)

因为

delayInNanoSeconds从 a隐式转换double为 anint64_t

这有多糟糕?

4

1 回答 1

3

一旦您将时间从秒(以 表示delayInSeconds)转换为纳秒(以 表示delayInNanoSeconds),您就不需要 a 的额外精度,double并且转换为 a 是安全的long long。您可以在调用中将其转换为dispatch_time()

double delayInSeconds = 2.0;
double delayInNanoSeconds = delayInSeconds * NSEC_PER_SEC;

dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW,
                                        (int64_t)delayInNanoSeconds);

dispatch_after(popTime, dispatch_get_main_queue(), ^(void){});

这应该会导致警告消失。或者,您可以更改delayInNanoSeconds.

于 2012-06-20T15:06:52.463 回答