Re: Passing a swift function to objective-c
Re: Passing a swift function to objective-c
- Subject: Re: Passing a swift function to objective-c
- From: Kevin Meaney <email@hidden>
- Date: Fri, 17 Oct 2014 17:59:45 +0100
Thanks Roland.
On 17 Oct 2014, at 00:20, Roland King <email@hidden> wrote:
>
>> On 17 Oct 2014, at 6:13 am, Kevin Meaney <email@hidden> wrote:
>>
>> Hi,
>>
>> I'm beginning to feel this above my pay grade as I can't seem to work it out.
>>
>> I have a framework in Objective-C. I've been writing some tests for it, and to make life fun I've been writing the tests in Swift.
>>
>> I have a property of a class in the objective-c framework declared like so:
>>
>> @property (nonatomic, copy) CGImageRef (^createImage)(NSDictionary *);
>>
>> In my tests I'm trying to write a function that I can assign to the property.
>>
>> In the most basic form the function looks like this, I'm ignoring the passed in dictionary for the purposes of asking this question:
>>
>> func createCGImage2(dictionary: [NSObject:AnyObject]) -> CGImage {
>> let jpegURL = NSBundle.mainBundle().URLForResource("myimage", withExtension:"jpg")
>> let imageSource = CGImageSourceCreateWithURL(jpegURL, nil)!
>> let theImage = CGImageSourceCreateImageAtIndex(imageSource, 0, nil)
>> return theImage
>> }
>>
>> When I try to set the property with the function I get:
>>
>> error: cannot convert the expression's type '([NSObject : AnyObject]) -> Unmanaged<CGImage>!' to type '(([NSObject : AnyObject]!) -> Unmanaged<CGImage>!)!?'
>>
>> Now, if I have a function declared like so:
>>
>> func createCGImageFromDictionary(dictionary: [NSObject:AnyObject]!) -> Unmanaged<CGImage>!
>>
>> error: cannot convert the expression's type '([NSObject : AnyObject]!) -> Unmanaged<CGImage>!' to type '(([NSObject : AnyObject]!) -> Unmanaged<CGImage>!)!?'
>>
>> I'm still a long way off. There's an extra () in there and an extra !? at the end that I really don't know how to interpret. Also I don't know how to convert the result of CGImageSourceCreateImageAtIndex to a Unmanaged<CGImage> as a return value.
>>
>> I've been through various bits of the documentation, but something isn't clicking so that I understand how this stuff works so that I can try and solve it.
>>
>> Kevin
>>
>> __
>
> ok this simple test works for me .. I also don’t understand the extra parens etc in your example. So where does your code differ from the below? Note I set it both with a public function and a closure, just to see if it works.
A detail I should have included. The @property is declared in the optional section of a protocol. I know the object in question has implemented the property. But that explains the extra requirements for unwrapping.
I've put together a gist where I've attempted to play with this.
https://gist.github.com/SheffieldKevin/a06907e163885f249548
I got the assigning to a property working when that property was declared as part of the class, but not when it has been declared in the optional section of a protocol. So I was able to duplicate what you did Roland. But no matter what I try, documentation I read I can't make it work when the property is declared in the protocol.
I got myself distracted because the project I setup to try out stuff was an objective-c command line tool and I also had trouble calling swift code from Objective-c. I could not get a swift function that wasn't a class or instance method to be callable from Objective-c.
Kevin
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden