Calling objective-C typedef block from swift

主宰稳场 提交于 2019-12-07 18:56:43

问题


I'm trying to call a method from swift. The method is in a singleton written in objective-C

the block in the header file:

typedef void(^VPersonResultBlock)(Person *person, NSError *error);

- (void)askForMe:(VPersonResultBlock)block;

and here's the implementation of that method.

- (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);
        }
    }];
}
}

In Objective-C, I can call this and it autocompletes without confusion.

[[VDataStore instance] askForMe:^(Person *person, NSError *error) {
    // do things with myself that aren't strange
}];

Now let's say I want to call the same method from swift. The bridging header is setup, with the header file imported, but swift's expectation is confusing.

VDataStore.askForMe(VDataStore)

This is what shows up in the autocomplete options

(VPersonResultBlock!) -> Void askForMe(self: VDataStore)

what I was hoping for, was for this to autocomplete into a closure, and although it appears to see all of the information correctly, what it's expecting isn't lining up with what objective-C seems to understand.

How do I call this properly from swift?


回答1:


Directly translate your ObjC calling code to Swift is

VDataStore.instance().askForMe() {
    person, error in 
    // do things with myself that aren't strange
}

Your problem is that askForMe is instance method but you are accessing from class object VDataStore.askForMe. Swift will give you a function object that takes an instance as input.



来源:https://stackoverflow.com/questions/26286160/calling-objective-c-typedef-block-from-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!