DarkLeafyGreen
DarkLeafyGreen

Reputation: 70406

Expected identifier and ARC problems when re-using code

I copied some classes from an older project into my new project. I had to remove all release/autorelease statements but I still have some errors:

 NSArray *allContacts = [(__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook)];
    for (int i =0; i < allContacts.count; i++) {
        ABRecordRef person = [allContacts objectAtIndex:i];

In the first line I get Expected identifier. In the third line I get implicit conversion of an Objective-C pointer to 'ABRecordRef (aka 'cpmnst void *)' is disallowed with ARC.

Any ideas how to fix this?

Upvotes: 2

Views: 1604

Answers (1)

user557219
user557219

Reputation:

Consider:

NSArray *allContacts = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

A function whose name contains Copy returns a reference that’s owned by the caller, which means that the caller must release it. Under ARC, releasing is automatic but you need to tell ARC that the reference returned by that function is owned by the caller because the function declaration lacks that information. This is an example of ownership transfer and you should use __bridge_transfer in this case:

NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

In:

ABRecordRef person = [allContacts objectAtIndex:i];

-objectAtIndex: returns an object that is not owned by the caller. Since you’re assigning that object to a non-object type (ABRecordRef), you need a simple bridge cast:

ABRecordRef person = (__bridge ABRecordRef)[allContacts objectAtIndex:i];

Upvotes: 6

Related Questions