我需要创建一个foo将抛出闭包作为参数的函数.我可以使用Swift或ObjC实现它,但我需要能够从两者中调用它.
像这样:
// Swift
func bar() throws
func foo(_ block: () throws -> void)
foo {
try bar()
}
Run Code Online (Sandbox Code Playgroud)
和
// Objc
[self foo:^(
[other barBar];
)];
Run Code Online (Sandbox Code Playgroud)
我尝试用Swift和ObjC实现它而没有成功.使用Swift:
@objc
func foo(block: () throws -> Void)
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
方法不能标记为@objc,因为参数1的类型不能在Objective-C中表示
如果我尝试用ObjC实现它:
typedef BOOL (^ThrowingBlock)(NSError **);
- (void)foo:(ThrowingBlock)block;
Run Code Online (Sandbox Code Playgroud)
然后它不会转换为抛出的块(就像使用函数一样):
func foo(_: (NSErrorPointer) -> Bool)
Run Code Online (Sandbox Code Playgroud)
知道怎么做到这一点?