With ARC, I can no longer cast CGColorRef to id. I learned that I need to do a bridged cast. According clang docs:
A bridged cast is a C-style cast annotated with one of three keywords:
(__bridge T) opcasts the operand to the destination typeT. IfTis a retainable object pointer type, thenopmust have a non-retainable pointer type. IfTis 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) opcasts 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) opcasts 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_retainedor__bridge_transfercast purely to convince ARC to emit an unbalanced retain or release, respectively, is poor form.
In what kind of situations would I use each?
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.