iOS 5阻止ARC桥接演员 [英] iOS 5 Blocks ARC bridged cast

查看:98
本文介绍了iOS 5阻止ARC桥接演员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题引用了这个问题:
如何使用块简化回调逻辑?

This Question references this Question: How to simplify callback logic with a Block?

我的标题包含这些typedef

My header has these typedefs

typedef void (^StuffDoneBlock)(NSDictionary * parsedData);
typedef void (^StuffFailedBlock)(NSError * error);

在init

stuffDoneCallback = Block_copy(done);
StuffFailedCallback = Block_copy(error);

在本文中,它说Block_copy是不必要的。但是它需要一个桥接演员。
编译器消息如下:

In this paper its says that Block_copy is unnecessary. But then it needs a bridged cast. The compiler message is at follows:

error: cast of block pointer type 'StuffDoneBlock' (aka 'void (^)(NSDictionary *__strong)') to C pointer type 'const void *' requires a bridged cast [4]
         stuffDoneCallback = _bridge(Block_copy(done));
                                     ^~~~~~~~~~~~~~~~
/Developer-4.2/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/usr/include/Block.h:60:61: note: instantiated from:
 #define Block_copy(...) ((__typeof(__VA_ARGS__))_Block_copy((const void *)(__VA_ARGS__)))
                                                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~


推荐答案

首先,为什么你甚至使用 Block_copy()?除非您正在编写原始C,否则您应该在块上调用 -copy ,如 [完成副本] 。其次,ARC将为您复制需要超过其初始化范围的块[1],因此您甚至不需要再调用 -copy 。唯一的例外是块类型属性仍然需要 copy 属性。

First off, why are you even using Block_copy()? Unless you're writing raw C, you should be calling -copy on the block instead, as in [done copy]. Secondly, ARC will copy blocks for you that need to live past their initialization scope[1], so you don't need to even call -copy anymore. The only "exception" is that block-typed properties still need to have the copy attribute.

[1] :这里似乎需要澄清。当编译器看到它需要超过其初始化范围时,ARC仅隐式复制块。这基本上意味着它被分配给一个逃避当前范围的变量(在父范围,实例变量,静态等中声明的堆栈变量)。但是,如果它作为参数传递给方法/函数,则编译器不会执行任何自动复制。通常这不是问题,因为需要通过堆栈帧( dispatch_async(),完成块等)保持块的块感知方法/函数将为你复制它们。但是,块感知的API(例如 NSArray )将不会隐式复制块,因为它们期望一个简单的 -retain 会做到这一点。如果您将块传递给非块感知API并且块需要超过当前范围,则必须使用显式 -copy

[1]: Clarification appears to be needed here. ARC only implicitly copies blocks when the compiler sees that it needs to live past its initialization scope. This basically means when it's assigned to a variable that escapes the current scope (stack variable declared in a parent scope, instance variable, static, etc.). However, if it's passed as an argument to a method/function, the compiler does not do any automatic copying. Typically this isn't a problem because block-aware methods/functions that need to hold onto the block past the stack frame (dispatch_async(), completion blocks, etc.) will copy them for you. However, APIs that are not block-aware (such as NSArray) will not implicitly copy the block, since they expect that a simple -retain will do the trick. If you're passing your block to a non-block-aware API and the block needs to live past the current scope, you must use an explicit -copy.

这篇关于iOS 5阻止ARC桥接演员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆