问题
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