苦苦挣扎将Objective C选择器和目标签名转换为Swift [英] Struggling to convert Objective C selector and target signature to Swift

查看:118
本文介绍了苦苦挣扎将Objective C选择器和目标签名转换为Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,

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

I am trying to convert an Objective C snippet to Swift. I understand the selector can be translated directly by placing it in a string, but I cannot understand the Objective C signature.:

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

The Objectice C selector (2nd parameter):

UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

目标:

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{

我的问题是:
1.我只需将选择器传递给:

My Questions are: 1.Can I simply pass the selector as :

UIImageWriteToSavedPhotosAlbum(image, self, "image:didFinishSavingWithError:contextInfo:", nil);

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

2.Please help me with the singnature of the target function. I am stumped!

推荐答案

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

To convert from an Objective-C method name to Swift the first parameter name in the Objective C method becomes the function name and then the remaining parameters become the parameters of the function.

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

In your case, the first parameter name is image, so the function name in Swift will be image.

所以,

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo

变得有些奇怪 -

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

}

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

To make things a bit simpler, you an use a different internal parameter name for the error -

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

}

这篇关于苦苦挣扎将Objective C选择器和目标签名转换为Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆