gpt4 book ai didi

ios - 添加 kABPersonAddressProperty 时 ABAddressBookSave 崩溃

转载 作者:行者123 更新时间:2023-11-29 10:49:43 24 4
gpt4 key购买 nike

我正在制作一个应用程序,我需要在其中将联系人保存到地址簿中。一切正常,除了当我添加 kABPersonAddressProperty 时,我首先添加它们然后保存地址,但在保存时它崩溃了。

我得到的错误是:

-[__NSCFString count]: unrecognized selector sent to instance 0x99e6f30
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString count]: unrecognized selector sent to instance 0x99e6f30'

这是我使用的代码:

ABRecordRef aRecord = ABPersonCreate();
CFErrorRef anError = NULL;
//
//some code here, not relevant
//
ABMutableMultiValueRef multiAdd = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiAdd, (__bridge CFStringRef)self.Street.text, kABPersonAddressStreetKey, NULL);
ABMultiValueAddValueAndLabel(multiAdd, (__bridge CFStringRef)self.ZIPcode.text, kABPersonAddressZIPKey, NULL);
ABMultiValueAddValueAndLabel(multiAdd, (__bridge CFStringRef)self.City.text, kABPersonAddressCityKey, NULL);

ABRecordSetValue(aRecord, kABPersonAddressProperty, multiAdd, &anError);
CFRelease(multiAdd);

//More irrelevant code here


ABAddressBookRef addressBook;
CFErrorRef error = NULL;
addressBook = ABAddressBookCreateWithOptions(nil, NULL);

BOOL isAdded = ABAddressBookAddRecord (addressBook, aRecord, &error);

if(isAdded){
NSLog(@"added..");
}
if (error != NULL) {
NSLog(@"ABAddressBookAddRecord %@", error);
}

error = NULL;

BOOL isSaved = ABAddressBookSave (addressBook, &error);

每当我运行此代码时,错误始终为 NULL,并且 isAdded 始终为真,但在执行 ABAddressBookSave(addressBook,&error); 时它仍然崩溃另一件重要的事情是,如果我删除这部分代码:

ABMutableMultiValueRef multiAdd = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiAdd, (__bridge CFStringRef)self.Street.text, kABPersonAddressStreetKey, NULL);
ABMultiValueAddValueAndLabel(multiAdd, (__bridge CFStringRef)self.ZIPcode.text, kABPersonAddressZIPKey, NULL);
ABMultiValueAddValueAndLabel(multiAdd, (__bridge CFStringRef)self.City.text, kABPersonAddressCityKey, NULL);

ABRecordSetValue(aRecord, kABPersonAddressProperty, multiAdd, &anError);
CFRelease(multiAdd);

联系人添加得很好,包括名字、姓氏、多个电话号码、URL 和电子邮件。

最佳答案

地址属性不是kABMultiStringPropertyType,而是kABMultiDictionaryPropertyType

要解决崩溃问题,请尝试通过传递正确的类型 kABMultiDictionaryPropertyType 来更改对 ABMultiValueCreateMutable 的调用。

然后您还需要通过从地址字符串值创建字典来更新填充地址信息的方式。查看this post举个例子。

它应该看起来像这样(未经测试):

ABMultiValueRef addresses = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSDictionary *values = [NSDictionary dictionaryWithObjectsAndKeys:
self.Street.text, (NSString *)kABPersonAddressStreetKey,
self.ZIPcode.text, (NSString *)kABPersonAddressZIPKey,
self.City.text, (NSString *)kABPersonAddressCityKey,
nil];

ABMultiValueAddValueAndLabel(addresses, (CFDictionaryRef)values, kABHomeLabel, NULL);
ABRecordSetValue(aRecord, kABPersonAddressProperty, addresses, &anError);

关于ios - 添加 kABPersonAddressProperty 时 ABAddressBookSave 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21002571/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com