3

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!

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
weePee
  • 895
  • 1
  • 8
  • 23
  • 1
    Did you try it? Does it work? – jtbandes Aug 03 '14 at 20:41
  • @jtbandes : Sorry I am unsure what you mean? What should I try? – weePee Aug 03 '14 at 20:45
  • Part of your question is "Can I do _x_?" Did you try _x_? What problem are you having? – jtbandes Aug 03 '14 at 20:47
  • Yes I did. I received an error : 'NSInvalidArgumentException ... does not respond to selector image:didFinishSavingWithError:contextInfo:' – weePee Aug 03 '14 at 20:50
  • Ok... still more information is needed. What class is `self`? Did you implement the `image:didFinishSavingWithError:contextInfo:` method? Please show the relevant parts of your code. You can [edit](http://stackoverflow.com/posts/25108888/edit) this information into your question. – jtbandes Aug 03 '14 at 20:51
  • 'self' is a viewcontroller. I didn't implement the method , I don't know what the signature should be. Thats my main problem – weePee Aug 03 '14 at 20:53
  • Have you read [this guide](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html#//apple_ref/doc/uid/TP40014216-CH4-XID_27)? – jtbandes Aug 03 '14 at 20:54
  • 1
    Hi there, yes I have and I have implemented a few selectors. All with no parameters in the target function. So thats easy for me, I dont know objective C well enough to translate "- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{" into Swift – weePee Aug 03 '14 at 20:59

1 Answers1

8

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

}
luk2302
  • 55,258
  • 23
  • 97
  • 137
Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • This is a wonderful solution. I spent hours trying to figure this out before stumbling upon this answer. Is it possible to have a function name that differs from the first argument, and reference that through the Selector() call? – NolanDC Nov 29 '14 at 17:11
  • `UIImage *` and `NSError *` in Objective-C should become `UIImage!` and `NSError!` in swift. What you have is not safe. – user102008 May 17 '15 at 04:55
  • @user102008: You are correct.. keep in mind this question was asked and answered during Swift Beta, a month after it was initially released. – weePee Aug 09 '15 at 06:21