Struggle for converting Objective C selector and target signature in Swift
Good day,
I am trying to convert an Objective C snippet to Swift. I understand that a selector can be translated directly by putting it on a string, but I cannot understand the Objective C signature:
Objectice C selector (2nd parameter):
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
Purpose:
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
My questions are: 1.Can I just pass the selector as:
UIImageWriteToSavedPhotosAlbum(image, self, "image:didFinishSavingWithError:contextInfo:", nil);
2. Please help me with target function synchronicity. I'm stumped!
source to share
To convert from an Objective-C method name to Swift, the first parameter name in the Objective C method becomes the function name, and the remaining parameters become the function parameters.
In your case, the first is the parameter name image
, so the function name in Swift would be image
.
So,
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
getting a little weird -
func image(image: UIImage, didFinishSavingWithError: NSError, contextInfo:UnsafePointer<Void>) {
}
To make things a little easier, you use a different internal parameter name for the error -
func image(image: UIImage, didFinishSavingWithError error: NSError, contextInfo:UnsafePointer<Void>) {
}
source to share