iosabaddressbookabrecordref

How would I bridge cast NSNumber to ABRecordID in iOS


I start learning programming with Objective-C, and do not have much knowledge of C. So, bridge casting in iOS 6 is still somewhat confusing for me.

Here is the scenario:

I have a person ABRecordID stored in CoreData as 'NSNumber' attribute. Later on, I would like to access to that person directly, so I want to use the person's ABRecordID to access the contact info using ABAddressBook. Noticed that ABAddressBookGetPersonWithRecordID needs ABRecordID, below is how I perform casting in my code...

address_book = ABAddressBookCreate();
ABRecordID rec_id = (__bridge ABRecordID)person.record_id;

However, This was not successful and I was given incompatible types casting 'int' into 'ABRecordID' (aka 'int') with a __bridge cast.

Already confused as it is, what would be a proper way to bridge cast between ARC type and CF type?

Also, in which case should I use (__bridge retained) instead of (__bridge)?


Solution

  • The ABRecordID is a synonym (typedef) for int32_t which is a 32-bit integer. So typecasting is not the proper approach. You want to create an NSNumber with the value of the id.

    ABRecordId rec_id = person.record_id;
    NSNumber *wrapper = [NSNumber numberWithInt:(int)rec_id];
    

    and later:

    NSNumber *wrapper = ...
    ABRecordId rec_id = (ABRecordId)[wrapper intValue];
    

    Please note that bridging and ARC are irrelevant when going to/from ABRecordId and NSNumber. There is no use of any of the CF types in this case. You'll know when you are using CF types because you'll have a variable whose type begins with CF (such as CFArrayRef).