How do I move NSNumber to ABRecordID in iOS

I'm starting to learn programming with Objective-C and I don’t have much knowledge of C. So, bridge casting in iOS 6 is still a bit confusing for me.

Here is the scenario:

I have an ABRecordID person stored in CoreData as an NSNumber attribute. Later, I would like to access this person directly, so I want to use the ABRecordID person to access contact information using ABAddressBook. Noticed that ABAddressBookGetPersonWithRecordID requires ABRecordID, the following describes how I cast in my code ...

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

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

Already confusing, be that as it may, what would be the appropriate way to transition between ARC types and CF type?

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

+4
source share
1 answer

ABRecordID is a synonym (typedef) for int32_t , which is a 32-bit integer. Therefore, casting is not the right approach. You want to create an NSNumber with an id value.

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

and later:

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

Note that bridges and ARCs do not matter when moving to / from ABRecordId and NSNumber. In this case, one of the CF types is not used. You will know when you use CF types because you will have a variable whose type begins with CF (for example, CFArrayRef ).

+14
source

All Articles