[英]How to convert an Objective-C Block into a Swift Closure?
I have the following Objective-C block assignment example from Github dzenbot/DZNPhotoPickerController that I am struggling to convert to Swift. 我有下面的Github dzenbot / DZNPhotoPickerController的 Objective-C块分配示例,我正努力将其转换为Swift。 Controller is a UIImagePickerController.
控制器是一个UIImagePickerController。
controller.finalizationBlock = ^(UIImagePickerController *picker, NSDictionary *info) {
UIImage *image = info[UIImagePickerControllerEditedImage];
weakSelf.imageView.image = image;
// Dismiss when the crop mode was disabled
if (picker.cropMode == DZNPhotoEditorViewControllerCropModeNone) {
[weakSelf dismissController:picker];
}
};
I have tried the following, but cannot figure out where I am going wrong. 我尝试了以下方法,但无法弄清楚我要去哪里。
controller.finalizationBlock = { (picker: UIImagePickerController, info: NSDictionary) -> UIImagePickerControllerFinalizationBlock! in
var image = UIImagePickerControllerEditedImage(info)
imageView.image = image
// Dismiss when the crop mode was disabled
if (picker.cropMode == .None) {
dismissEditor(picker)
}
}
The error I get is: 我得到的错误是:
"Cannot assign value of type '(UIImagePickerController, NSDictionary) -> UIImagePickerControllerFinalizationBlock!'
“无法分配类型'(UIImagePickerController,NSDictionary)-> UIImagePickerControllerFinalizationBlock的值! to type 'UIImagePickerControllerFinalizationBlock!'
键入“ UIImagePickerControllerFinalizationBlock!”
The Objective-C block does not return anything, so the closure for the same should look like this: Objective-C块不返回任何内容,因此对它的关闭应如下所示:
controller.finalizationBlock = { (picker, info) in
var image = UIImagePickerControllerEditedImage(info)
imageView.image = image
// Dismiss when the crop mode was disabled
if (picker.cropMode == .None) {
dismissEditor(picker)
}
}
The Objective-C block returns nothing, Objective-C块什么也不返回,
controller.finalizationBlock = { (picker: UIImagePickerController, info: NSDictionary) -> (Void) in
var image = UIImagePickerControllerEditedImage(info)
imageView.image = image
// Dismiss when the crop mode was disabled
if (picker.cropMode == .None) {
dismissEditor(picker)
}
}
Note 1. If you're going to use the closure in other functions you can declare an Alias in order to provides more readable code (But It's not mandatory) 注意1.如果要在其他函数中使用闭包,则可以声明一个Alias以便提供更具可读性的代码(但这不是强制性的)
typealias UIImagePickerControllerFinalizationBlock = (picker: UIImagePickerController, info: NSDictionary) -> (Void)
Note 2. Maybe you can make the info parameter type more " Swifty " with [String: AnyObject]
or whatever the type info parameter uses. 注2:也许你可以用info参数类型的更多“SWIFTY”
[String: AnyObject]
或任何类型的信息参数使用。
Try this it may help you : 试试这个可能对您有帮助:
controller.finalizationBlock = {(picker: UIImagePickerController, info: [NSObject : AnyObject]) -> Void in
var image: UIImage = info[UIImagePickerControllerEditedImage]
weakSelf.imageView.image = image
// Dismiss when the crop mode was disabled
if picker.cropMode == DZNPhotoEditorViewControllerCropMode.None
{
weakSelf.dismissController(picker)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.