Skip to content

Instantly share code, notes, and snippets.

@SheffieldKevin
Created October 17, 2014 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SheffieldKevin/a06907e163885f249548 to your computer and use it in GitHub Desktop.
Save SheffieldKevin/a06907e163885f249548 to your computer and use it in GitHub Desktop.
Objective-C properties of protocols that take blocks/closures and accessing them in Swift
//
// CreateImageProtocol.h
// test
//
// Created by Kevin Meaney on 17/10/2014.
// Copyright (c) 2014 Kevin Meaney. All rights reserved.
//
#import <Foundation/Foundation.h>
@protocol CreateImageProtocol <NSObject>
-(NSInteger)imageWidth;
@optional
@property (nonatomic, copy) CGImageRef(^createImage)(NSDictionary *);
@end
id<CreateImageProtocol>MakeImageProvider();
//
// ImageProvider.h
// test
//
// Created by Kevin Meaney on 17/10/2014.
// Copyright (c) 2014 Kevin Meaney. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "CreateImageProtocol.h"
@interface ImageProvider : NSObject <CreateImageProtocol>
@end
//
// ImageProvider.m
// test
//
// Created by Kevin Meaney on 17/10/2014.
// Copyright (c) 2014 Kevin Meaney. All rights reserved.
//
#import "ImageProvider.h"
@implementation ImageProvider
@synthesize createImage;
-(NSInteger)imageWidth
{
if (self.createImage)
{
CGImageRef myImage = self.createImage(
@{ @"imagefilepath" : @"/Users/ktam/Pictures/julieanneInNZ.jpg" });
if (myImage)
return CGImageGetWidth(myImage);
}
return -1;
}
@end
id<CreateImageProtocol>MakeImageProvider()
{
return [[ImageProvider alloc] init];
}
//
// ImageProvider2.h
// test
//
// Created by Kevin Meaney on 17/10/2014.
// Copyright (c) 2014 Kevin Meaney. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface ImageProvider2 : NSObject
-(NSInteger)imageWidth;
@property (nonatomic, copy) CGImageRef(^createImage)(NSDictionary *);
@end
//
// ImageProvider2.m
// test
//
// Created by Kevin Meaney on 17/10/2014.
// Copyright (c) 2014 Kevin Meaney. All rights reserved.
//
#import "ImageProvider2.h"
@implementation ImageProvider2
@synthesize createImage;
-(NSInteger)imageWidth
{
if (self.createImage)
{
CGImageRef myImage = self.createImage(
@{ @"imagefilepath" : @"/Users/ktam/Pictures/julieanneInNZ.jpg" });
if (myImage)
return CGImageGetWidth(myImage);
}
return -1;
}
@end
//
// main.m
// test
//
// Created by Kevin Meaney on 17/10/2014.
// Copyright (c) 2014 Kevin Meaney. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "test-Swift.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// NSInteger theWidth = getImageWidth2();
NSInteger theWidth = ClassMethodWrapper.getImageWidth;
printf("Image width: %ld\n", theWidth);
// insert code here...
// NSLog(@"Hello, World!");
}
return 0;
}
//
// SupplyCreateImage.swift
// test
//
// Created by Kevin Meaney on 17/10/2014.
// Copyright (c) 2014 Kevin Meaney. All rights reserved.
//
import Foundation
public func makeImage(dict : [ NSObject:AnyObject]! ) -> Unmanaged<CGImageRef>! {
let thePath = dict["imagefilepath"]! as String
let jpegURL = NSURL.fileURLWithPath(thePath, isDirectory: false)
let imageSource = CGImageSourceCreateWithURL(jpegURL, nil)!
let theImage = CGImageSourceCreateImageAtIndex(imageSource, 0, nil)
let x = Unmanaged.passUnretained(theImage);
// x.autorelease()
return x
}
/*
public func getImageWidth() -> Int {
var imageProvider:AnyObject = MakeImageProvider()
imageProvider.setCreateImage?(makeImage)
imageProvider.imageWidth()
}
*/
public func getImageWidth2() -> Int {
let imageProvider = ImageProvider2()
imageProvider.createImage = makeImage
return imageProvider.imageWidth()
}
// public class ClassMethodWrapper : NSObject {
class ClassMethodWrapper : NSObject {
// public class func getImageWidth() -> Int {
class func getImageWidth() -> Int {
let imageProvider = ImageProvider2()
imageProvider.createImage = makeImage
return imageProvider.imageWidth()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment