我正在尝试从 swift 调用一个方法。
该方法是用 Objective-C 编写的单例
头文件中的块:
typedef void(^VPersonResultBlock)(Person *person, NSError *error);
- (void)askForMe:(VPersonResultBlock)block;
这是该方法的实现。
- (void)askForMe:(VPersonResultBlock)block
{
if (_me) block(_me,nil);
else {
[Person getMeWithBlock:^(PFObject *person, NSError *error) {
if (!error) {
_me = (Person *)person;
block(_me,nil);
}
else if (error) {
block(nil,error);
}
else {
NSDictionary *userInfo = @{
NSLocalizedDescriptionKey: NSLocalizedString(@"Operation was unsuccessful.", nil),
NSLocalizedFailureReasonErrorKey: NSLocalizedString(@"The operation failed to retrieve the user.", nil),
NSLocalizedRecoverySuggestionErrorKey: NSLocalizedString(@"Check your network connection and try again", nil)
};
NSError *error = [[NSError alloc] initWithDomain:@"VisesAsyncErrorDomain" code:-10 userInfo:userInfo];
block(nil,error);
}
}];
}
}
在 Objective-C 中,我可以调用它,它会自动完成而不会造成混淆。
[[VDataStore instance] askForMe:^(Person *person, NSError *error) {
// do things with myself that aren't strange
}];
现在假设我想从 swift 调用相同的方法。桥接头已设置,并导入了头文件,但 swift 的期望令人困惑。
VDataStore.askForMe(VDataStore)
这是自动完成选项中显示的内容
(VPersonResultBlock!) -> Void askForMe(self: VDataStore)
我所希望的是自动完成为一个闭包,尽管它似乎正确地看到了所有信息,但它所期望的与 Objective-C 似乎理解的并不相符。
我如何从 swift 正确调用它?