-3

This is what I currently do:

Basically I have these 2 rules when doing bridging

Convert from CF land to NS land, use CFBridgingRelease Convert from NS land to CF land, use __bridge.

I've never seen a sample code from apple doing it differently.

Here is a sample:

ABRecordRef ABWhatIsThis= (__bridge ABRecordRef)([self.allMyContacts objectAtIndex:indexPath.row]);

NSString * strName= CFBridgingRelease (ABRecordCopyCompositeName (ABWhatIsThis));

The reasoning is when I want to convert something from NS land to CF land, I do not want to change ownership. The ARC will maintain retain count on the NS land and keep the object in the CF land alive till it's NS land counter part is destroyed.

That's why I used ABRecordRef ABWhatIsThis= (__bridge ABRecordRef)

If I convert something from CF land to NS land, I will use CFBridgingRelease. The reasoning is that once I got an ARC object pointing to the CFBridging, I would not to have CFBridging to retain the stuff.

However, I am still confused. Is there a hard rule when I should use CFBridgingRelease?

Also I noticed there is no such thing as CFBridging. Only CFBridgingRelease and CFBridgingRetain. I wonder why the imbalance.

user4951
  • 32,206
  • 53
  • 172
  • 282
  • 1
    This has been covered a million times. Possible dupe: http://stackoverflow.com/questions/7036350/arc-and-bridged-cast, http://stackoverflow.com/questions/6822473/correct-bridging-for-arc, http://stackoverflow.com/questions/14352494/bridged-cast-bridge-transfer-vs-bridge-with-synthesized-setter, – CodaFi Mar 28 '13 at 09:01

1 Answers1

0

__bridge transfers a pointer between Objective-C and Core Foundation with no transfer of ownership.

__bridge_retained or CFBridgingRetain casts an Objective-C pointer to a Core Foundation pointer and also transfers ownership to you. You use this functions to cast an Objective-C object as Core Foundation-style object and take ownership of the object so that you can manage its lifetime. You are responsible for subsequently releasing the object

Burhanuddin Sunelwala
  • 5,318
  • 3
  • 25
  • 51