地址簿中存储的所有电话号码都有如下标签: “家”,“工作”,“家庭传真”等 我需要更改名为“ATM”的自定义标签的特定联系人的标签。
我收到此错误消息:
"Assertion failed: (((ABCMultiValue *)multiValue)->flags.isMutable), function ABMultiValueReplaceLabelAtIndex, file /SourceCache/AddressBook_Sim/AddressBook-796.6/ABMultiValue.c, line 118."
这是我的代码,我得到的评论除了:
- (void)displayPerson:(ABRecordRef)person {
NSString* name = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
self.firstName.text = name;
NSString* phone = nil;
NSString* lbl = nil;
NSString* newLbl = @"ATM";
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person,
kABPersonPhoneProperty);
if (ABMultiValueGetCount(phoneNumbers) > 0) {
phone = (__bridge_transfer NSString*)
ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
lbl = (__bridge_transfer NSString*)
ABMultiValueCopyLabelAtIndex(phoneNumbers, 0);
//*** HERE IS THE PROBLEM ***
ABMultiValueReplaceLabelAtIndex(phoneNumbers, CFSTR("ATM"), 0);
NSLog(@" - %@ (%@)", phone, lbl);
} else {
phone = @"[None]";
lbl = @"[None]";
newLbl = @"[None]";
}
CFRelease(phoneNumbers);
}
如何更改该电话号码的标签?
答案 0 :(得分:2)
我找到了一个解决方案: 下面的代码工作正常:
- (void)displayPerson:(ABRecordRef)person
{
ABAddressBookRef ab = ABAddressBookCreate();
ABRecordRef record = ABAddressBookGetPersonWithRecordID(ab, ABRecordGetRecordID(person));
CFErrorRef *error = NULL;
ABMultiValueRef phoneNumbers = ABRecordCopyValue(record,kABPersonPhoneProperty);
ABMutableMultiValueRef phoneNumberMV = ABMultiValueCreateMutableCopy(phoneNumbers);
for(CFIndex i=0; i < ABMultiValueGetCount(phoneNumberMV); i++){
NSString *phoneNumber = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneNumberMV,i);
//NSSlog(@"phoneNumber = %@", phoneNumber);
if ([@"1128558994" isEqualToString:phoneNumber]) {
//now delete it!!! ;-)
/*
NSSlog(@"phoneNumbers = %@",phoneNumbers);
NSSlog(@"index = %d", i);
*/
//BOOL didRemove = ABMultiValueRemoveValueAndLabelAtIndex(phoneNumberMV,i);
BOOL didChanged = ABMultiValueReplaceLabelAtIndex(phoneNumberMV, (CFStringRef)@"0870", i);
NSLog(@"didRemove = %@\n", (didChanged ? @"TRUE" : @"FALSE"));
BOOL didSet = ABRecordSetValue(record, kABPersonPhoneProperty, phoneNumberMV, nil);
NSLog(@"didSet = %@\n", (didSet ? @"TRUE" : @"FALSE"));
//and save it!
BOOL didSave = ABAddressBookSave(ab, (CFErrorRef *) error);
NSLog(@"didSave = %@\n", (didSave ? @"TRUE" : @"FALSE"));
if (error) {
NSLog(@"ABAddressBookSaveError = %@", error);
}
}
}
CFRelease(ab);
}