我有一个混合的ObjC和Swift iOS项目。
我有一个用于在运行时测试平凡闭包注入的类:
@objc
class TyphoonClosureTester: NSObject {
@objc var closure: (() -> Void)?
@objc
override init() {}
@objc
init(closure: (() -> Void)?) {
self.closure = closure
}
@objc
func callClosure() {
guard let closure = closure else {
assert(false, "no closure 1")
return
}
closure()
NSLog("Have called closure 1 OK")
}
}
我的台风程序汇编文件包含以下功能:
func testAClosure(closure: @escaping () -> Void) -> AnyObject {
return TyphoonDefinition.withClass(TyphoonClosureTester.self) { definition in
definition?.useInitializer(#selector(TyphoonClosureTester.init(closure:))) { initializer in
initializer?.injectParameter(with: closure)
}
definition?.scope = .prototype
} as AnyObject
}
我尝试如下使用此闭包:
let closureTester1: TyphoonClosureTester = assembler.testAClosure(closure: {
NSLog(" closure 1 called!")
}) as! TyphoonClosureTester
// causes EXC_BAD_ACCESS
closureTester1.callClosure()
但是当我调用闭包时会导致EXC_BAD_ACCESS
。 (确切的消息是Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
。因此,似乎该闭包正在释放到某个位置。
我尝试将捕获添加到程序集(即[closure]
)中,但这没有什么区别:
func testAClosure(closure: @escaping () -> Void) -> AnyObject {
return TyphoonDefinition.withClass(TyphoonClosureTester.self) { [closure] definition in
definition?.useInitializer(#selector(TyphoonClosureTester.init(closure:))) { [closure] initializer in
initializer?.injectParameter(with: closure)
}
definition?.scope = .prototype
} as AnyObject
}
我还尝试将闭包作为属性而不是通过init注入,这没有什么区别。
答案 0 :(得分:0)
不支持此功能。您可以尝试在测试用例中查找示例。
此处介绍了注入包装的基元:https://github.com/appsquickly/typhoon/wiki/wrap-primitive-values-into-NSValue
。 。但是没有引用块/闭包。
如果描述您想要实现的目标,则可能会建议一个下一个最佳解决方法。