Reputation: 427
I've just about completed the integration of a photo library based on Objective-C into my code, but I'm stuck when trying to re-write the Objective-C example code into Swift in one spot in particular. Here is the Objective-C code causing the problem.
__weak MHGalleryController *blockGallery = gallery;
gallery.finishedCallback = ^(NSInteger currentIndex,UIImage *image,MHTransitionDismissMHGallery *interactiveTransition,MHGalleryViewMode viewMode){
NSIndexPath *newIndex = [NSIndexPath indexPathForRow:currentIndex inSection:0];
[self.tableView scrollToRowAtIndexPath:newIndex atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
dispatch_async(dispatch_get_main_queue(), ^{
UIImageView *imageView = [(ImageTableViewCell*)[self.tableView cellForRowAtIndexPath:newIndex] iv];
[blockGallery dismissViewControllerAnimated:YES dismissImageView:imageView completion:nil];
});
};
My assumption is that I need to set the finishedCallback variable to a closure with parameters - similar to the Block above.As such I tried to do the same thing. My block is referenced as a variable "closingBlockInputs" below.
weak var blockedGallery = gallery
var closingBlock = {
(currentIndex:NSInteger, image:UIImage, interactiveTransition: MHTransitionDismissMHGallery, viewMode: MHGalleryViewMode) -> () in
}
var tr = MHTransitionDismissMHGallery()
gallery.finishedCallback = closingBlock(1, UIImage(name:"temp"),tr,MHGalleryViewMode.OverView)
However, when I run the code I get an error like:
() is not convertible to Int, UIImage, MHTransitionDismissMHGallery, MHGalleryViewMode
I'm pretty sure I have the general flow right, just missing something...
Any help would be greatly appreciated...
Upvotes: 0
Views: 220
Reputation: 37043
The finishedCallback
is of type (NSInteger, UIImage, MHTransitionDismissMHGallery, MHGalleryViewMode) -> ()
, that is, a closure that takes four input parameters and returns Void
. In your Swift code, you are calling closingBlock
and trying to assign its return value (Void
) to finishedCallback
, which is why the error is telling you that ()
(aka Void
) is not convertible to the closure's type.
There are a few other things worth noting. UIImage(name: "temp")
returns an optional UIImage
, but the closure is expecting a non-optional UIImage
. The init?(name:)
initialiser is failable since there might not be an image file with the specified name. So make sure you unwrap the optional UIImage
before passing it as a parameter to the closure.
Next thing to consider is that the types of the closure parameters are inferred by the compiler, so there's no need to write them explicitly.
Also, Swift resolves strong reference cycles in closures using capture lists, rather than by declaring a separate weak
version of a variable (see Strong Reference Cycles for Closures in The Swift Programming Language for details of this mechanism).
So I'd expect your code to simply look like this in Swift:
gallery.finishedCallback = { [unowned gallery] currentIndex, image, interactiveTransition, viewMode in
// ...
}
Upvotes: 4