Skip to content

Instantly share code, notes, and snippets.

@keitaito
Created March 9, 2016 08:45
Show Gist options
  • Save keitaito/07fe97de694ea0575707 to your computer and use it in GitHub Desktop.
Save keitaito/07fe97de694ea0575707 to your computer and use it in GitHub Desktop.
Get value from CFDictionaryRef by converting it to NSDictionary.
+ (NSString *)getValueFromCFDictionaryRef {
// Assume you have record ID.
NSInteger recordID;
// Get a person record.
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, recordID);
// Get addresses data from person. Watch out it's a multi-value property.
ABMultiValueRef addresses = ABRecordCopyValue(person, kABPersonAddressProperty);
// Get the first one. Convert it from CFDictionaryRef to NSDictionary.
NSDictionary *address = (NSDictionary *)CFBridgingRelease(ABMultiValueCopyValueAtIndex(addresses, 0));
// Convert a dictionary key too, and get its value.
NSString *city = address[(NSString *)CFBridgingRelease(kABPersonAddressCityKey)];
// Don't forget to release objects.
CFRelease(addresses);
CFRelease(addressBook);
return city;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment