的iOS API函数UIImageWriteToSavedPhotosAlbum
需要选择作为一个argment:无法识别UIImageWriteToSavedPhotosAlbum中的Swift Selector,为什么?
func UIImageWriteToSavedPhotosAlbum(_ image: UIImage,
_ completionTarget: Any?,
_ completionSelector: Selector?,
_ contextInfo: UnsafeMutableRawPointer?)
https://developer.apple.com/documentation/uikit/1619125-uiimagewritetosavedphotosalbum
然而,在迅速,当我调用该函数,选择永远不会被认可:
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):https://stackoverflow.com/questions/41093735/using-contextinfo-unsaferawpointer-in-uiimagewritetosavedphotosalbum-swift-3 – Larme
@Larme Swift 3.我用#选择器()',也不起作用。 – NeoWang