简体   繁体   中英

Create swift closure and pass it to objective-c class as completion block

Now I'm adding Swift code on objective-C based iOS application.

But I have a problem in calling the objective-C method from Swift side.

I'd like to get and draw image on UIImageView in View or ViewController which is written with Swift. On the other hand my wrapper class of Photos Library is written with objective-C.

My wrapper class of Photos Library is below.

・MyPhotoLibraryMgr.h

typedef void (^AppPhotoLibraryGetImageCompletion)(UIImage *image, NSError *error);

- (void)getImageWithTarget:(NSInteger)index
                targetSize:(CGSize)targetSize
                completion:(AppPhotoLibraryGetImageCompletion)completion;

・MyPhotoLibraryMgr.m

- (void)getImageWithTarget:(NSInteger)index
                targetSize:(CGSize)targetSize
                completion:
(AppPhotoLibraryGetImageCompletion)completion
{
    PHAsset *asset = _myAsetsList[index];

    PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
    options.resizeMode = PHImageRequestOptionsResizeModeFast;
    options.deliveryMode = PHImageRequestOptionsDeliveryModeOpportunistic;

    [[PHImageManager defaultManager] requestImageForAsset:asset
                                               targetSize:targetSize
                                              contentMode:PHImageContentModeAspectFill
                                                  options:options
                                            resultHandler:^(UIImage *result, NSDictionary *info) {
                                                NSError *error = [info objectForKey:PHImageErrorKey];
                                                if (error) {
                                                    NSLog(@"error=%@", error);
                                                }
                                                completion(result, error);
                                            }];
}

I can get and draw image in View or ViewController written by objective-C like below,

・MyViewController.m

AppPhotoLibraryMgr *photoLibMgr = [AppPhotoLibraryMgr sharedInstance];
AppPhotoLibraryGetImageCompletion completion = ^(UIImage *image, NSError *error) {
                imageView.image = image;
};

[[AppPhotoLibraryMgr sharedInstance] getImageWithTarget:index
                                             targetSize:imageView.frame.size
                                             completion:completion];

But I can't get image in View or ViewController written in Swift.

・MySwiftViewController.swift

let completion = {(image: UIImage, error: NSError) -> AppPhotoLibraryGetImageCompletion in
    let imageView = UIImageView(image: image)
}

photoLibMgr?.getImageWithTarget(index, targetSize: size, completion: completion)

Error message from Xcode is


Cannot convert value of type 'UIImage, NSError -> AppPhotoLibraryGetImageCompletion" to expected argument type 'AppPhotoLibraryGetImageCompletion'

The block typedef void (^AppPhotoLibraryGetImageCompletion)(UIImage *image, NSError *error); in objective-c has return type void .

But the counter part in your Swift has return type other than Void . The completion block should be like:

let completion = {(image: UIImage, error: NSError) -> Void in
    ...
    ...
}

Use typealias for making your closure reusable.

Example:

typealias AppPhotoLibraryGetImageCompletion = (UIImage, NSError) -> Void

let completion: AppPhotoLibraryGetImageCompletion = { (image, error) in
    ...
    ...
}

Edit: Different way

photoLibMgr?.getImageWithTarget(index, targetSize: size, completion: { (image, error) in
    ...
    ...
})

Your declaration of completion variable is wrong in MySwiftViewController.swift file.
Edit it like this:

let completion: AppPhotoLibraryGetImageCompletion = { (image, error) in
   let imageView = UIImageView(image: image)
}

OR
If you don't want to declare completion handler as variable, then simply do the:

LinphoneLoginController.shared().getImageWithTarget(index, targetSize: size) { (image, error) in
    let imageView = UIImageView(image: image)
}

It should be typedef instead of Typedef . Move typedef on top of interface declare and don't declare it again:

typedef void (^AppPhotoLibraryGetImageCompletion)(UIImage *image, NSError *error);

@interface MyPhotoLibraryMgr....

After the changes it should work similar in Swift , auto complete will change your AppPhotoLibraryGetImageCompletion to (image, error) {..}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM