-
(__bridge_transfer <NSType>) opor alternativelyCFBridgingRelease(op)is used to consume a retain-count of aCFTypeRefwhile transferring it over to ARC. This could also be represented byid someObj = (__bridge <NSType>) op; CFRelease(op); -
(__bridge_retained <CFType>) opor alternativelyCFBridgingRetain(op)is used to hand anNSObjectover to CF-land while giving it a +1 retain count. You should handle aCFTypeRefyou create this way the same as you would handle a result ofCFStringCreateCopy(). This could also be represented byCFRetain((__bridge CFType)op); CFTypeRef someTypeRef = (__bridge CFType)op; -
__bridgejust casts between pointer-land and Objective-C object-land. If you have no inclination to use the conversions above, use this one.
above is summary
A bridged cast is a C-style cast annotated with one of three keywords:
(__bridge T) opcasts 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 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 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.
Using a __bridge_retained or __bridge_transfer cast purely to convince ARC to emit an unbalanced retain or release, respectively, is poor form.
Objective-C与ARC之间的桥接转换详解
本文详细解释了Objective-C与ARC之间的桥接转换,包括转移所有权、增加引用计数以及不同类型的桥接使用场景。通过示例代码帮助开发者理解如何在Objective-C与ARC环境间高效转换对象。
3405

被折叠的 条评论
为什么被折叠?



