简体   繁体   English

iPhone中的电话联系人中没有电话号码时,程序崩溃

[英]program crashes when there is no phone number in phone contacts in iphone

i am able to read contacts from phone book and display them in tableview ...... 我能够从电话簿中读取联系人并将其显示在表格视图中……

the program is working successful when there is both phone number and namr in contact list and crashes if there is no name or phonenumber in contact list.... 当联系人列表中同时包含电话号码和姓名时,程序运行成功;如果联系人列表中没有名称或电话号码,则程序崩溃。

can any one please help me how to stop that and how to check these condition to display those names in table view which contain both phone number and name.... and my code is as follows 谁能帮助我,如何阻止这种情况以及如何检查这些条件,以便在包含电话号码和姓名的表格视图中显示这些名称。...我的代码如下

    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;

You have to check whether person has contact number in list or not.. Try to use below modified code 您必须检查人员列表中是否有联系电话。尝试使用以下修改后的代码

wantedname=[[NSMutableArray alloc] init];

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

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

NSString *name; NSString *名称;

for (id person in thePeople) 为(人中的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]; importcontacts * phonecontacts = [[importimports分配] init]; phonecontacts.names = wantedname; phonecontacts.names = wantedname; phonecontacts.phonenumbers = wantednumber; phonecontacts.phonenumbers =所需电话号码;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM