I’m confused with bridge and bridge_transfer, is this correct?
-(void)getData{
ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *allPeople = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSString *name;
for ( int i = 0; i < [allPeople count]; i++ )
{
name = (__bridge_transfer NSString *) ABRecordCopyValue((__bridge ABRecordRef)[allPeople objectAtIndex:i], kABPersonFirstNameProperty);
}
CFRelease(addressBook);
allPeople = nil;
}
Is there anyone who can explain me how to use them?
If you have automatic-reference-counting (ARC) turned on, the code is correct.
There are two
__bridge_transferin your statements. As a result, the ownership of created CFObjects will be transferred to NSObjects. If you have ARC turned on, they will be freed up automatically. If you used__bridgeinstead for these 2 statements, you will need to explicitly callCFReleaseto release CFObjects created by the*CopyAPI.The
__bridgestatement is also correct. Because you are referencing an NSObject in CF API. You are not transferring ownership, so that ARC will free it up.