gpt4 book ai didi

ios - 如何保存一个姓名字符串和两个不同长度的电子邮件和电话号码数组

转载 作者:行者123 更新时间:2023-11-28 21:41:44 24 4
gpt4 key购买 nike

我想这是一个非常简单和基本的问题。

我正在使用 #import <AddressBook/AddressBook.h>获取设备的联系信息,我已经成功地实现了这一点。

当我从每个联系人那里收到一个电话号码和一封电子邮件时,一切正常。但是当我收到多个电话号码和多个电子邮件时,我的应用程序就会崩溃。我想我没有使用正确的保存方法。

所以我真的很想知道如何将一个姓名字符串和一组电子邮件(不同长度)和电话号码(不同长度)保存作为一个联系人的组,对所有其他联系人也是如此。这样以后在详细信息屏幕上重现结果就不难了

 CFErrorRef *error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);

for(int i = 0; i < numberOfPeople; i++) {

ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );

NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
[values1 addObject:[NSString stringWithFormat:@"%@ %@",firstName,lastName]]; //values1 array to save name of contact

ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
for (CFIndex j=0; j < ABMultiValueGetCount(emails); j++) {
NSString* email = (__bridge NSString*)ABMultiValueCopyValueAtIndex(emails, j);
NSLog(@"Email:%@", email);
[values2 addObject:[NSString stringWithFormat:@"%@",email]];
} //values2 array to save email associated to that contact

ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
NSString *phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
NSLog(@"phone:%@", phoneNumber);
[values3 addObject:[NSString stringWithFormat:@"%@",phoneNumber]];
}//values3 array to save different phone no's associated to that contacts


NSDictionary *dict = [[NSDictionary alloc]init];

}

现在我有三个数组,详细信息包含一个联系人的详细信息。现在如何将这三个数组值合并为一个,以便轻松保存和获取多个或数百个联系人的数据

最佳答案

ABAddressBookRef allPeople = ABAddressBookCreate();
CFArrayRef allContacts = ABAddressBookCopyArrayOfAllPeople(allPeople);
CFIndex numberOfContacts = ABAddressBookGetPersonCount(allPeople);

NSLog(@"numberOfContacts------------------------------------%ld",numberOfContacts);


for(int i = 0; i < numberOfContacts; i++){
NSString* name = @"";
NSString* phone = @"";
NSString* email = @"";

ABRecordRef aPerson = CFArrayGetValueAtIndex(allContacts, i);
ABMultiValueRef fnameProperty = ABRecordCopyValue(aPerson, kABPersonFirstNameProperty);
ABMultiValueRef lnameProperty = ABRecordCopyValue(aPerson, kABPersonLastNameProperty);

ABMultiValueRef phoneProperty = ABRecordCopyValue(aPerson, kABPersonPhoneProperty);\
ABMultiValueRef emailProperty = ABRecordCopyValue(aPerson, kABPersonEmailProperty);

NSArray *emailArray = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(emailProperty);
NSArray *phoneArray = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(phoneProperty);


if (fnameProperty != nil) {
name = [NSString stringWithFormat:@"%@", fnameProperty];
}
if (lnameProperty != nil) {
name = [name stringByAppendingString:[NSString stringWithFormat:@" %@", lnameProperty]];
}

if ([phoneArray count] > 0) {
if ([phoneArray count] > 1) {
for (int i = 0; i < [phoneArray count]; i++) {
phone = [phone stringByAppendingString:[NSString stringWithFormat:@"%@\n", [phoneArray objectAtIndex:i]]];
}
}else {
phone = [NSString stringWithFormat:@"%@", [phoneArray objectAtIndex:0]];
}
}

if ([emailArray count] > 0) {
if ([emailArray count] > 1) {
for (int i = 0; i < [emailArray count]; i++) {
email = [email stringByAppendingString:[NSString stringWithFormat:@"%@\n", [emailArray objectAtIndex:i]]];
}
}else {
email = [NSString stringWithFormat:@"%@", [emailArray objectAtIndex:0]];
}
}

NSLog(@"NAME : %@",name);
NSLog(@"PHONE: %@",phone);
NSLog(@"EMAIL: %@",email);
NSLog(@"\n");
}

如果联系人有,您可以在日志中看到多个联系人和电子邮件。

关于ios - 如何保存一个姓名字符串和两个不同长度的电子邮件和电话号码数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31782029/

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