Skip to content

Instantly share code, notes, and snippets.

@nuclearace
Last active August 29, 2015 14:18
Show Gist options
  • Save nuclearace/5d3d1737002f12be6109 to your computer and use it in GitHub Desktop.
Save nuclearace/5d3d1737002f12be6109 to your computer and use it in GitHub Desktop.
Breaks Swift
#import <Foundation/Foundation.h>
@class MyTester;
@interface Breaker: NSObject
@property (nonatomic) MyTester* tester;
- (void)breakSwift;
@end
#import <Foundation/Foundation.h>
#import "blockerror-Swift.h"
#import "Breaker.h"
@implementation Breaker: NSObject
-(id)init {
if (!(self = [super init])) {
return nil;
}
MyTester* tester = [[MyTester alloc] init];
self.tester = tester;
[self breakSwift];
return self;
}
-(void)breakSwift {
__block int i;
[[self tester] addAck:0](^(NSArray* data) {
NSLog(@"breaing swift");
i = 1 + 1;
NSLog(@"%i", i);
});
}
@end
#import "Breaker.h"
import Foundation
public typealias Callback = (NSArray) -> Void
struct AckMap {
private var acks = [Int: Callback]()
mutating func addAck(num:Int, callback:Callback) {
self.acks[num] = callback
}
mutating func executeAck(ack:Int, items:[AnyObject]) {
self.acks[ack]?(items)
self.acks.removeValueForKey(ack)
}
}
public class MyTester: NSObject {
var acks = AckMap()
public func addAck(num:Int) -> (Callback) -> Void {
return {[weak self] callback in
callback([0])
self?.acks.addAck(num, callback: callback)
return
}
}
public func breakObjc(num:Int) {
self.acks.executeAck(num, items: ["break"])
}
}
let breaker = Breaker()
breaker.tester.breakObjc(0)
@lilyball
Copy link

lilyball commented Apr 8, 2015

Change Callback to

public typealias Callback = @objc_block (NSArray?) -> Void

and the crash goes away.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment