我对Objective C很陌生并且面临以下两难困境。我希望在类预测中的方法完成运行后运行一段代码。
-(void)populate: (void (^)())completionHandler{//method that should run first
//code that runs first
}
我在主函数
中调用此方法[prediction populate:^{
NSLog(@"it works")//it works is new displayed
}];
我希望在populate方法完成运行后立即显示“it works”。但这不会发生。我究竟做错了什么?
答案 0 :(得分:5)
只需在方法中添加completionHandler
参数即可自动运行。当您完成populate:
方法中需要做的任何事情时,您需要自己调用它:
-(void)populate: (void (^)())completionHandler { //method that should run first
//code that runs first
if (completionHandler) {
completionHandler();
}
}