• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Passing a swift function to objective-c
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Passing a swift function to objective-c


  • Subject: Passing a swift function to objective-c
  • From: Kevin Meaney <email@hidden>
  • Date: Thu, 16 Oct 2014 23:13:45 +0100

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

_______________________________________________

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


  • Follow-Ups:
    • Re: Passing a swift function to objective-c
      • From: Roland King <email@hidden>
  • Prev by Date: Re: Problem getting import UTI recognised
  • Next by Date: Re: UICollectionView parallax-like horizontal scrolling
  • Previous by thread: Re: NSTableView inside NSTextView
  • Next by thread: Re: Passing a swift function to objective-c
  • Index(es):
    • Date
    • Thread