【发布时间】:2020-10-28 11:03:15
【问题描述】:
iOS API Function UIImageWriteToSavedPhotosAlbum 将选择器作为一个参数:
func UIImageWriteToSavedPhotosAlbum(_ image: UIImage,
_ completionTarget: Any?,
_ completionSelector: Selector?,
_ contextInfo: UnsafeMutableRawPointer?)
https://developer.apple.com/documentation/uikit/1619125-uiimagewritetosavedphotosalbum
但是,在 swift 中,当我调用此函数时,选择器永远不会被识别:
class Base {
func save_image(img:UIImage) {
UIImageWriteToSavedPhotosAlbum(img, self, Selector("image:didFinishSavingWithError:contextInfo:"), nil)
// I also tried this:
// UIImageWriteToSavedPhotosAlbum(img, self, #selector(image(_:didFinishSavingWithError:contextInfo:))
}
@objc func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
print("Photo Saved Successfully")
}
}
class Child:Base {
}
// This is how I call the save_image function:
let child = Child()
child.save_image()
如您所见,我尝试从签名和字符串构造选择器,但都不起作用。我总是在运行时收到此错误:
'XXX.Child' does not implement methodSignatureForSelector: -- trouble ahead
Unrecognized selector ......
这里发生了什么?我想知道这是否是因为 swift 没有看到 Child 类的方法,因为该方法是从 Base 类继承的?
如何才能成功通过选择器?
我已阅读的相关问题:
【问题讨论】:
-
你的 Swift 版本是什么?例如,如果您使用 Swift 3,则应该使用
#selector()。另外,这里有解决方案(如果你使用 Swift 3):stackoverflow.com/questions/41093735/… -
@Larme Swift 3。我用了
#selector(),也不起作用。