Created
October 17, 2014 16:38
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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