ARC 和桥接铸件

时间:2023-02-14
本文介绍了ARC 和桥接铸件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 ARC,我不能再将 CGColorRef 转换为 id.我了解到我需要进行桥接演员表.根据 clang 文档:

With ARC, I can no longer cast CGColorRef to id. I learned that I need to do a bridged cast. According clang docs:

桥接演员表是使用以下三个关键字之一注释的 C 风格演员表:

A bridged cast is a C-style cast annotated with one of three keywords:

(__bridge T) op 将操作数转换为目标类型 T.如果 T是一个可保留的对象指针类型,那么 op 必须有一个不可保留的指针类型.如果 T 是不可保留的指针类型,那么 op 必须有一个可保留的对象指针类型.否则演员表格式不正确.没有所有权转移,ARC插入没有保留操作.

(__bridge T) op casts the operand to the destination type T. If T is a retainable object pointer type, then op must have a non-retainable pointer type. If T is a non-retainable pointer type, then op must have a retainable object pointer type. Otherwise the cast is ill-formed. There is no transfer of ownership, and ARC inserts no retain operations.

(__bridge_retained T) op 转换操作数,它必须有可保留对象指针类型,指向目标类型,必须是不可保留的指针类型.ARC 保留价值,但须遵守通常对局部值进行优化,接收者负责用于平衡 +1.

(__bridge_retained T) op casts the operand, which must have retainable object pointer type, to the destination type, which must be a non-retainable pointer type. ARC retains the value, subject to the usual optimizations on local values, and the recipient is responsible for balancing that +1.

(__bridge_transfer T) op 转换操作数,它必须有不可保留的指针类型,指向目标类型,它必须是可保留对象指针类型.ARC 会在最后释放值封闭的完整表达式,服从通常的优化当地价值观.

(__bridge_transfer T) op casts the operand, which must have non-retainable pointer type, to the destination type, which must be a retainable object pointer type. ARC will release the value at the end of the enclosing full-expression, subject to the usual optimizations on local values.

需要这些转换才能将对象移入和移出电弧控制;参见关于转换的部分的基本原理可保留的对象指针.

These casts are required in order to transfer objects in and out of ARC control; see the rationale in the section on conversion of retainable object pointers.

使用 __bridge_retained__bridge_transfer 转换纯粹是为了说服ARC分别发出不平衡的保留或释放很差表格.

Using a __bridge_retained or __bridge_transfer cast purely to convince ARC to emit an unbalanced retain or release, respectively, is poor form.

我会在什么样的情况下使用它们?

In what kind of situations would I use each?

例如,CAGradientLayer 有一个 colors 属性,该属性接受 CGColorRef 数组.我的猜测是我应该在这里使用 __brige,但我应该(或不应该)的确切原因尚不清楚.

For example, CAGradientLayer has a colors property which accepts an array of CGColorRefs. My guess is that I should use __brige here, but exactly why I should (or should not) is unclear.

推荐答案

我同意描述令人困惑.由于我刚刚掌握了它们,所以我将尝试总结一下:

I agree that the description is confusing. Since I just grasped them, I'll try to summarize:

  • (__bridge_transfer <NSType>) opCFBridgingRelease(op) 用于消耗 CFTypeRef同时将其转移到ARC.这也可以用 id someObj = (__bridge <NSType>) op; 来表示.CFRelease(op);

  • (__bridge_transfer <NSType>) op or alternatively CFBridgingRelease(op) is used to consume a retain-count of a CFTypeRef while transferring it over to ARC. This could also be represented by id someObj = (__bridge <NSType>) op; CFRelease(op);

(__bridge_retained <CFType>) op 或者 CFBridgingRetain(op) 用于将 NSObject 交给 CF-land 同时给它 +1 保留计数.您应该像处理 CFStringCreateCopy() 的结果一样处理您创建的 CFTypeRef.这也可以用 CFRetain((__bridge CFType)op); 来表示.CFTypeRef someTypeRef = (__bridge CFType)op;

(__bridge_retained <CFType>) op or alternatively CFBridgingRetain(op) is used to hand an NSObject over to CF-land while giving it a +1 retain count. You should handle a CFTypeRef you create this way the same as you would handle a result of CFStringCreateCopy(). This could also be represented by CFRetain((__bridge CFType)op); CFTypeRef someTypeRef = (__bridge CFType)op;

__bridge 只是在指针域和 Objective-C 对象域之间进行转换.如果您不想使用上述转换,请使用此转换.

__bridge just casts between pointer-land and Objective-C object-land. If you have no inclination to use the conversions above, use this one.

也许这会有所帮助.我自己,我更喜欢 CFBridging… 宏而不是普通的强制转换.

Maybe this is helpful. Myself, I prefer the CFBridging… macros quite a bit over the plain casts.

这篇关于ARC 和桥接铸件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!