我有3个条件需要实现如下,
if(condition 1)
{
return true;
}
else
{
if(condition 2 )
{
//after 10 sec delay call condition 3 which will return BOOL value
retutn that BOOL value after 10 sec
}
else
{
return false;
}
}
如何在延迟后获取 BOOL 值作为返回类型?
最佳答案
我能想到的最简单的方法是使用 blocks 。像这样声明方法:
-(void)methodWithDelay:(void(^)(BOOL result))aCompletion
{
if(condition 1)
{
aCompletion(YES);
}
else
{
if(condition 2 )
{
//after 10 sec delay call condition 3 which will return BOOL value
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
aCompletion(YES/NO);
});
}
else
{
aCompletion(NO);
}
}
}
然后像这样使用它:
[self methodWithDelay:^(BOOL result) {
//do what you want with the result
}];
请注意提供的有关 block 的文档,以及如何避免它们的内存问题。
关于ios - 如何在Delay后调用返回类型为BOOL的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29466781/