title: ARC and bridged cast
body: <p>With ARC, I can no longer cast <code>CGColorRef</code> to <code>id</code>. I learned that I need to do a bridged cast. According <a href="http://clang.llvm.org/docs/AutomaticReferenceCounting.html#bridged-casts" rel="noreferrer">clang docs</a>:</p>
  
  <blockquote>
    <p>A <strong>bridged cast</strong> is a C-style cast annotated with one of three keywords:</p>
    
    <p><code>(__bridge T) op</code> casts the operand to the destination type <code>T</code>. If <code>T</code>
    is a retainable object pointer type, then <code>op</code> must have a
    non-retainable pointer type. If <code>T</code> 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.</p>
    
    <p><code>(__bridge_retained T) op</code> 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.</p>
    
    <p><code>(__bridge_transfer T) op</code> 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.</p>
    
    <p>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.</p>
    
    <p>Using a <code>__bridge_retained</code> or <code>__bridge_transfer</code> cast purely to convince
    ARC to emit an unbalanced retain or release, respectively, is poor
    form.</p>
  </blockquote>
  
  <p>In what kind of situations would I use each?</p>
  
  <p>For example, <code>CAGradientLayer</code> has a <code>colors</code> property which accepts an array of <code>CGColorRef</code>s. My guess is that I should use <code>__brige</code> here, but exactly why I should (or should not) is unclear.</p>