2017-09-25 114 views
0

的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类继承的?

如何成功传递选择器?

相关的问题我已阅读:

@selector() in Swift?

+0

什么是您的Swift版本?如果你使用Swift 3,你应该使用'#selector()'。此外,在这里你有解决方案(如果你使用Swift 3):https://stackoverflow.com/questions/41093735/using-contextinfo-unsaferawpointer-in-uiimagewritetosavedphotosalbum-swift-3 – Larme

+0

@Larme Swift 3.我用#选择器()',也不起作用。 – NeoWang

回答

1

提供一些指导你的选择,以帮助其找到合适的功能:

class Base { 
    func save_image(img:UIImage) { 
     UIImageWriteToSavedPhotosAlbum(img, self, #selector(Base.image(_:didFinishSavingWithError:contextInfo:)), nil) 
    } 

    @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() 
+0

我也试过这个。 – NeoWang

0

methodSignatureForSelector是NSObject的的方法。 所以,你需要继承NSObject类。

class Base: NSObject { 
    ... 
} 
相关问题