gpt4 book ai didi

iphone - 执行 ABAddressBookSave 时 EXC_BAD_ACCESS !

转载 作者:行者123 更新时间:2023-12-03 21:12:08 26 4
gpt4 key购买 nike

我正在尝试创建一个新联系人并将其添加到地址簿中,但是当我到达 ABAddressSave 代码行时,我得到了 EXC_BAD_ACCESS。我看不出我做错了什么,我启用了 NSZombie 来检查这是否是与内存相关的错误,但它没有发现任何错误。谁能告诉我这段代码有什么问题吗?预先感谢您!

    CFErrorRef error = NULL;

ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();

ABRecordRef newRecord = ABPersonCreate();

ABRecordSetValue(newRecord, kABPersonFirstNameProperty, @"Xxxxxx", &error);

ABRecordSetValue(newRecord, kABPersonURLProperty, @"Yyyyyy", &error);


//Add phone numbers to record

ABMutableMultiValueRef phones = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(phones, @"1-555-555-5555", kABWorkLabel, NULL);


ABRecordSetValue(newRecord, kABPersonPhoneProperty, phones, &error);

CFRelease(phones);

//Add email address to record

ABMutableMultiValueRef emails = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(emails, @"xxx_xxx@yahoo.com", kABWorkLabel, NULL);


ABRecordSetValue(newRecord, kABPersonEmailProperty, emails, &error);

CFRelease(emails);

ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);

NSMutableDictionary *addressDict = [[NSMutableDictionary alloc] init];

[addressDict setObject:@"xxx1" forKey:(NSString *)kABPersonAddressStreetKey];
[addressDict setObject:@"xxx2" forKey:(NSString *)kABPersonAddressCityKey];
[addressDict setObject:@"xxx3" forKey:(NSString *)kABPersonAddressStateKey];
[addressDict setObject:@"xxx4" forKey:(NSString *)kABPersonAddressZIPKey];

ABMultiValueAddValueAndLabel(multiAddress, addressDict, kABWorkLabel, NULL);


ABRecordSetValue(newRecord, kABPersonAddressProperty, multiAddress, &error);

CFRelease(multiAddress);
[addressDict release];


ABAddressBookAddRecord(iPhoneAddressBook, newRecord, &error);
ABAddressBookSave(iPhoneAddressBook, NULL);

if(error != nil){
NSLog(@"Error creating contact:%@", error);
}

最佳答案

我建议使用 Memory->Object Allocations 模板在 Instruments 中运行代码。它应该很快地告诉您哪个对象有问题以及什么内存释放导致了问题。

关于iphone - 执行 ABAddressBookSave 时 EXC_BAD_ACCESS !,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2494830/

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