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: Roland King <email@hidden>
- Date: Fri, 17 Oct 2014 07:20:05 +0800
> 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.
I think that’s how you get your Unmanaged object as well, but I’m not entirely sure. Swift seems to do better with some CoreFoundation types than others.
Note that took far more time messing about with fiddly bits of syntax than I wish it had, and I’ve only not touched Swift for a week or so.
obj-C bit, Banana.h
@interface Banana : NSObject
@property( nonatomic, copy ) CGImageRef(^createImage)(NSDictionary *dictionary);
@end
swift bit, Orange.swift
public func x( dict : [ NSObject:AnyObject]! ) -> Unmanaged<CGImageRef>!
{
let jpegURL = NSBundle.mainBundle().URLForResource("myimage", withExtension:"jpg")
let imageSource = CGImageSourceCreateWithURL(jpegURL, nil)!
let theImage = CGImageSourceCreateImageAtIndex(imageSource, 0, nil)
let x = Unmanaged.passUnretained(theImage);
x.autorelease()
return x
}
@objc public class Orange
{
var j = Banana()
public func doSomething()
{
j.createImage = x
j.createImage = {
(dict)->Unmanaged<CGImageRef> in
let jpegURL = NSBundle.mainBundle().URLForResource("myimage", withExtension:"jpg")
let imageSource = CGImageSourceCreateWithURL(jpegURL, nil)!
let theImage = CGImageSourceCreateImageAtIndex(imageSource, 0, nil)
let x = Unmanaged.passUnretained(theImage);
x.autorelease()
return x
}
}
}
_______________________________________________
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