无法将简单的Objective-C块转换为Swift

时间:2015-07-02 22:11:33

标签: objective-c swift

不确定我做错了什么。我正在使用此模块https://github.com/wenzhaot/InstagramPhotoPicker(块的头文件在这里https://github.com/wenzhaot/InstagramPhotoPicker/blob/master/TWPhotoPicker/TWPhotoPickerController.h

在Objective-C中,语法为:

photoPicker.cropBlock = ^(UIImage *image) {
    //do something
};

我尝试将其转换为swift,但我收到错误

photoPicker.cropBlock{ (image:UIImage) -> () in
    //Do something
    //error: cannot invoke 'cropBlock' with an argument list of type '((UIImage) -> ())'
}

如何让它发挥作用?

1 个答案:

答案 0 :(得分:2)

您只是缺少=标志。另外image是隐式解包的可选:

photoPicker.cropBlock = { (image: UIImage!) -> () in
    //Do something
}

photoPicker.cropBlock = { image in
    //Do something
}