嗨,这里是下面的函数,我从函数中收集NSString
名字和姓氏等值并在它的数组中分配
- (void)getPersonOutOfAddressBook
{
ABAddressBookRef addressBook = ABAddressBookCreate();
if (addressBook != nil)
{
NSLog(@"Succesful.");
NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSUInteger i = 0;
NSLog(@" Result :%d",[allContacts count]);
for (i = 0; i < [allContacts count]; i++)
{
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
NSMutableArray *name = [NSMutableArray array];
[name addObject:firstName];
[name addObject:lastName];
[self displaynames:name];
//NSLog(@"FirstNAme::::: LastNAME::::MobileNUMb::::EMail::: %@ %@",firstName,lastName);
ABMultiValueRef mobile=ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);
for (int k=0;k<ABMultiValueGetCount(mobile); k++)
{
NSString *mobileNo = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(mobile, k);
// NSLog(@"mobile number: %@",mobileNo);
}
}
-(void)displaynames:(NSMutableArray*)names{
for(NSMutableArray* name in names){
NSLog(@"MyResult:%@ %@",[names objectAtIndex:0],[names objectAtIndex:1]);
messageToSend = [messageToSend stringByAppendingString:[names objectAtIndex:0]];
messageToSend = [messageToSend stringByAppendingString:[names objectAtIndex:1]];
}
}
并且还发送数据
- (IBAction)sendContacts:(id)sender
{
NSData *textData = [self.messageToSend dataUsingEncoding:NSASCIIStringEncoding];
//send data to all connected devices
[self.session sendDataToAllPeers:textData withDataMode:GKSendDataReliable error:nil];
}
这里 messageToSend 是一个 NSMutableString
因此,在上面的代码中,我收集了所有名字和姓氏并将其放入数组中。当我在 iphone 中运行它时,它显示错误[__NSArrayM insertObject:atIndex:]: object cannot be nil'
在NSLog(@"Myresult");
它显示名字和姓氏两次之后。所以任何人都可以告诉我我哪里出错了,以及我如何收集电话号码作为 wel.我必须在上面的代码中做哪些更改以避免该错误并且它只显示几个名字全部。
它显示
2013-06-03 11:40:12.524 GetContacts[1173:1bb03] Succesful.
2013-06-03 11:40:12.539 getlist[1173:1bb03] Result :2
2013-06-03 11:40:12.567 getlist[1173:1bb03] MyResult:john cena
2013-06-03 11:40:12.568 getlist[1173:1bb03] Successsssss::::::
2013-06-03 11:40:12.569 getlist[1173:1bb03] MyResult:john cena
2013-06-03 11:40:12.571 getlist[1173:1bb03] Successsssss::::::
2013-06-03 11:40:12.734 getlist[1173:1bb03] MyResult:peter john
2013-06-03 11:40:12.735 getlist[1173:1bb03] Successsssss::::::
2013-06-03 11:40:12.736 getlist[1173:1bb03] MyResult:peter john
2013-06-03 11:40:12.737 getlist[1173:1bb03] Successsssss::::::