我有一个看起来像这样的Objective-C回调。
//APIClient.h
typedef void (^DetailSearchFinishedCallBack)(RestaurantDetailModel *detailModel);
//Called in a different class as follows.
@property (nonatomic, copy) DetailSearchFinishedCallBack detailSearchFinishedCallBack;
self.detailSearchFinishedCallBack(nil);
在Swift类中,我希望能够侦听此回调,但是在查找此调用的正确语法时遇到了麻烦。这是我的快速代码:
lazy var apiClient: APIClient =
{
var apiClient : APIClient = APIClient()
//TODO: Set up callback listener here.
return apiClient
}()
我检查了这篇文章:
What's the equiv. Swift Closure-Callback to Objective-C's block callback?
但是该块是在本地定义的,而我的块是在apiClient中定义的。如果有人可以帮助我,我将不胜感激。
最佳答案
我认为类似这样的东西应该可以代替TODO
apiClient.detailSearchFinishedCallBack = { detailModel in
// Do something with detailModel if not nil
}
detailModel的类型将被推断,但您也可以是明确的
apiClient.detailSearchFinishedCallBack = { (detailModel: RestaurantDetailModel!) in
// Do something with detailModel if not nil
}