当iphone中的手机通讯录中没有电话号码时程序崩溃

时间:2011-04-07 05:26:21

标签: iphone

我可以通过电话簿阅读联系人并在桌面视图中显示......

当联系人列表中有电话号码和namr时,程序正在成功运行,如果联系人列表中没有姓名或电话号码,则程序崩溃....

任何人都可以帮我解决这个问题,以及如何检查这些条件,在表格视图中显示包含电话号码和姓名的名字....  我的代码如下

    wantedname=[[NSMutableArray alloc] init];
wantednumber=[[NSMutableArray alloc] init];

ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *thePeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

NSString *name;

for (id person in thePeople)
{

    name = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSLog(@"!!!!!! name ---> %@",name);
    ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSString *beforenumber = (NSString *)ABMultiValueCopyValueAtIndex(multi, 0);
    NSLog(@" contacts:%@",beforenumber );
    NSString* removed1=[beforenumber stringByReplacingOccurrencesOfString:@"-"withString:@""];
    NSString* removed2=[removed1 stringByReplacingOccurrencesOfString:@")"withString:@""];
    NSString* removed3=[removed2 stringByReplacingOccurrencesOfString:@" "withString:@""];
    NSString* removed4=[removed3 stringByReplacingOccurrencesOfString:@"("withString:@""];
    NSString* removed5=[removed4 stringByReplacingOccurrencesOfString:@"+"withString:@""];


}

importcontacts *phonecontacts = [[importcontacts alloc] init];
phonecontacts.names = wantedname;
phonecontacts.phonenumbers = wantednumber;

1 个答案:

答案 0 :(得分:1)

您必须检查某人是否在列表中有联系号码。 尝试使用以下修改后的代码

wantedname=[[NSMutableArray alloc] init];

wantednumber = [[NSMutableArray alloc] init];

ABAddressBookRef addressBook = ABAddressBookCreate(); NSArray * thePeople =(NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

NSString * name;

for(人们中的id)

{

name = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSLog(@"!!!!!! name ---> %@",name);
ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);

int count1=ABMultiValueGetCount(multi);
NSLog(@"%d",count1);

if (count1!=0) 
{
      NSString *beforenumber = (NSString *)ABMultiValueCopyValueAtIndex(multi, 0);
      NSLog(@" contacts:%@",beforenumber );
      NSString* removed1=[beforenumber stringByReplacingOccurrencesOfString:@"-"withString:@""];
      NSString* removed2=[removed1 stringByReplacingOccurrencesOfString:@")"withString:@""];
      NSString* removed3=[removed2 stringByReplacingOccurrencesOfString:@" "withString:@""];
      NSString* removed4=[removed3 stringByReplacingOccurrencesOfString:@"("withString:@""];
      NSString* removed5=[removed4 stringByReplacingOccurrencesOfString:@"+"withString:@""];
}

}

importcontacts * phonecontacts = [[importcontacts alloc] init]; phonecontacts.names = wantedname; phonecontacts.phonenumbers = wantednumber;