問題描述
使用 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 CGColorRef
s. 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>) op
或CFBridgingRelease(op)
用于消耗CFTypeRef的保留計數代碼>同時將其轉移到ARC.這也可以用
id someObj = (__bridge <NSType>) op; 來表示.CFRelease(op);
(__bridge_transfer <NSType>) op
or alternativelyCFBridgingRelease(op)
is used to consume a retain-count of aCFTypeRef
while transferring it over to ARC. This could also be represented byid 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模板網!