gpt4 book ai didi

ios - 如何将 Objective-C block 转换为 Swift 闭包?

转载 作者:行者123 更新时间:2023-11-30 13:18:26 24 4
gpt4 key购买 nike

我有来自 Github 的以下 Objective-C block 分配示例 dzenbot/DZNPhotoPickerController我正在努力转换为 Swift。 Controller 是一个 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];
}
};

我尝试了以下方法,但无法弄清楚我哪里出了问题。

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

我得到的错误是:

"Cannot assign value of type '(UIImagePickerController, NSDictionary) -> UIImagePickerControllerFinalizationBlock!' to type 'UIImagePickerControllerFinalizationBlock!'

最佳答案

Objective-C block 不返回任何内容,因此其闭包应如下所示:

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

关于ios - 如何将 Objective-C block 转换为 Swift 闭包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38009847/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com