iosobjective-cswiftobjective-c-blocksswift-block

Accessing Objective-C block from Swift


I have this Objective-C code in FileManagerHelper:

+(void)getMyVideoObject:(NSString *)videoId completion:(void (^)(MyVideoObject *myVideoObject, NSError *error)) completionBlock
{

}

For calling from Objective-C:

 [FileManagerHelper getMyVideoObject:videoId completion:^(MyVideoObject *myVideoObject, NSError *error) {

}];

But how do I call this getMyVideoObject from Swift?

FileManagerHelper.getMyVideoObject( .....?


Solution

  • 1) Add into your bridging header (-Bridging-Header.h file generated when you add .swift file to Objective-C project)

    #import "FileManagerHelper.h"
    

    2) In swift code use the following call

    FileManagerHelper.getMyVideoObject("id") { (video, error) in
    
    }