有人可以__autoreleasing在下面的示例代码块中向我解释一下目的吗?
- (void)execute:(NSError * __autoreleasing *)error {
// do stuff, possibly assigning error if something went wrong
}
Run Code Online (Sandbox Code Playgroud)
我删除了__autoreleasing所有东西似乎仍然编译/运行正常.我开始使用obj-c后ARC,所以我从来没有真正学过/理解所有那些双下划线的东西.我已阅读ARC过渡指南,但我不完全了解他们的NSError示例.
我需要创建一个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)
知道怎么做到这一点?