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
隐含地从double
转换为int64_t
这有多糟糕?
答案 0 :(得分:3)
一旦您将时间从秒(以delayInSeconds
表示)转换为纳秒(以delayInNanoSeconds
表示),您就不需要double
的额外精度了安全转换为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
的类型。