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: Sat, 18 Oct 2014 18:31:59 +0100
I've now found an acceptable solution.
Rather than a property I've added an optional method to the protocol which then in its implementation assigns using the property, pretty much to make sure that the function/block is copied. I declared the method like so:
-(void)applyMyCreateImageFunction:(CGImageRef (^)(NSDictionary *))createImageFunction
And when calling it from the swift code I call it like so:
imageProvider.applyMyCreateImageFunction?(makeImage)
The question mark was necessary to evaluate whether the optional protocol method had been implemented or not.
Kevin
On 18 Oct 2014, at 18:18, Kevin Meaney <email@hidden> wrote:
> Hi Roland,
>
> Thanks for the followup. I've created a proper git rep on github rather than just a gist. Most of my github reps seem to be this kind of mini demonstration project and I was trying to avoid yet another one.
>
> https://github.com/SheffieldKevin/swift-objectivec
>
>> What version of Xcode are you using by the way, Swift is so flux-y at the moment it probably makes a difference.
>
> I'm now running release Yosemite and release Xcode 6.1. That appears to me to make no difference.
>
> I still can't assign to the optional property of my protocol.
>
> You can ignore the ImageProvider2 class, I've just included it for completeness. It doesn't have a protocol and just declares the property itself.
>
> So with my protocol I've done something similar to what you did if I've interpreted your discussion correctly. I've got two properties, one that is required and one that is optional.
>
> Everything works fine for the required property. But I still can't get the optional one to work.
>
> In ImageProvider.m you'll see that the MakeImageProvider function assigns a block to the optional property and in SupplyCreateImage.swift I've commented out the assignment. I've provided the example this way so that it can be seen that it works when done using objective-c. But if you comment out the assignment to the optional property of the block, and uncomment the line in the swift file you'll see that the command line tool no longer compiles.
>
> Like you I've tried the use of the optional and playing around with casting but I've not actually got the code to compile this way.
>
> 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
_______________________________________________
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