Create swift closure and pass it to objective-c cl

2019-09-16 14:42发布

问题:

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'

回答1:

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)
}


回答2:

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
    ...
    ...
})


回答3:

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) {..}