苦苦挣扎将Objective C选择器和目标签名转换为Swift

wee*_*Pee 3 objective-c selector ios swift

美好的一天,

我正在尝试将Objective C片段转换为Swift.我理解选择器可以通过将其放在一个字符串中直接翻译,但我无法理解Objective C签名:

Objectice C选择器(第二个参数):

UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
Run Code Online (Sandbox Code Playgroud)

目标:

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
Run Code Online (Sandbox Code Playgroud)

我的问题是:1.我可以简单地将选择器传递给:

UIImageWriteToSavedPhotosAlbum(image, self, "image:didFinishSavingWithError:contextInfo:", nil);
Run Code Online (Sandbox Code Playgroud)

2.请帮我解决目标函数的问题.我很难过!

Pau*_*w11 8

要从Objective-C方法名称转换为Swift,Objective C方法中的第一个参数名称将成为函数名称,然后其余参数将成为函数的参数.

在您的情况下,第一个参数名称是image,因此Swift中的函数名称将是image.

所以,

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
Run Code Online (Sandbox Code Playgroud)

变得有点奇怪 -

func image(image: UIImage, didFinishSavingWithError: NSError, contextInfo:UnsafePointer<Void>)       {

}
Run Code Online (Sandbox Code Playgroud)

为了使事情变得更简单,您可以使用不同的内部参数名称来表示错误 -

func image(image: UIImage, didFinishSavingWithError error: NSError, contextInfo:UnsafePointer<Void>)       {

}
Run Code Online (Sandbox Code Playgroud)