尝试更新通讯簿中的标签电话号码
Posted
技术标签:
【中文标题】尝试更新通讯簿中的标签电话号码【英文标题】:Trying to update label phone number in Address Book 【发布时间】:2013-07-26 20:51:44 【问题描述】:地址簿中存储的所有电话号码都有一个标签,例如: “家庭”、“工作”、“家庭传真”等。 我需要更改特定联系人的标签,放置一个名为“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);
如何更改该电话号码的标签?
【问题讨论】:
人,只是为了补充我的方法“displayPerson”来自这个例子:developer.apple.com/library/ios/#documentation/ContactData/… 【参考方案1】:我找到了解决方案: 下面的代码工作正常:
- (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);
【讨论】:
以上是关于尝试更新通讯簿中的标签电话号码的主要内容,如果未能解决你的问题,请参考以下文章