ios - Objective C Block - Convert Swift Not Working -


i've completed integration of photo library based on objective-c code, i'm stuck when trying re-write objective-c example code swift in 1 spot in particular. here objective-c code causing 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 need set finishedcallback variable closure parameters - similar block above.as such tried same thing. block referenced 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 run code error like:

() not convertible int, uiimage, mhtransitiondismissmhgallery, mhgalleryviewmode

i'm pretty sure have general flow right, missing something...

any appreciated...

the finishedcallback of type (nsinteger, uiimage, mhtransitiondismissmhgallery, mhgalleryviewmode) -> (), is, closure takes 4 input parameters , returns void. in swift code, calling closingblock , trying assign return value (void) finishedcallback, why error telling () (aka void) not convertible closure's type.

there few other things worth noting. uiimage(name: "temp") returns optional uiimage, closure expecting non-optional uiimage. init?(name:) initialiser failable since there might not image file specified name. make sure unwrap optional uiimage before passing parameter closure.

next thing consider types of closure parameters inferred compiler, there's no need write them explicitly.

also, swift resolves strong reference cycles in closures using capture lists, rather declaring separate weak version of variable (see strong reference cycles closures in swift programming language details of mechanism).

so i'd expect code in swift:

gallery.finishedcallback = { [unowned gallery] currentindex, image, interactivetransition, viewmode in     // ... } 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -