Struggling to convert Objective C selector and target signature to Swift

大兔子大兔子 提交于 2019-12-06 09:01:34

问题


Good Day,

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.:

The Objectice C selector (2nd parameter):

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

The target:

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

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

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

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


回答1:


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.

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

So,

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

becomes the slightly odd looking -

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>)       {

}


来源:https://stackoverflow.com/questions/25108888/struggling-to-convert-objective-c-selector-and-target-signature-to-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!