0

嗨,这里是下面的函数,我从函数中收集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::::::
4

3 回答 3

1

首先检查您放入数组中的数据是否为零,如果只是跳过它。

像这样的东西:

if(firstName != nil)
    [yourMutableArray addObject:firstname];

你明白了。

于 2013-06-03T06:21:29.560 回答
0

要访问大部分内容,请使用以下代码

            CFStringRef firstName = ABRecordCopyValue(record , kABPersonFirstNameProperty);
            CFStringRef LastName = ABRecordCopyValue(record, kABPersonLastNameProperty);
            CFStringRef companyRef = ABRecordCopyValue(record, kABPersonOrganizationProperty);
            CFDateRef birthDate = ABRecordCopyValue(record, kABPersonBirthdayProperty);


            CFStringRef prefixName = ABRecordCopyValue(record , kABPersonPrefixProperty);
            CFStringRef phoneticFirstName = ABRecordCopyValue(record , kABPersonFirstNamePhoneticProperty);
            CFStringRef phoneticLastName = ABRecordCopyValue(record , kABPersonLastNamePhoneticProperty);
            CFStringRef middleName = ABRecordCopyValue(record , kABPersonMiddleNameProperty);
            CFStringRef suffixName = ABRecordCopyValue(record , kABPersonSuffixProperty);
            CFStringRef nickName = ABRecordCopyValue(record , kABPersonNicknameProperty);
            CFStringRef jobTitle = ABRecordCopyValue(record , kABPersonJobTitleProperty);
            CFStringRef department = ABRecordCopyValue(record , kABPersonDepartmentProperty);
            CFStringRef notes = ABRecordCopyValue(record , kABPersonNoteProperty);



            NSString *LastNameStr = (NSString *)LastName;
            NSString *firstNameStr = (NSString *)firstName;
            NSString *companyStr = (NSString *)companyRef;
            NSDate *birthDateValue = (NSDate *)birthDate;

            NSString *prefixNameStr = (NSString *)prefixName;
            NSString *phoneticFirstNameStr = (NSString *)phoneticFirstName;
            NSString *phoneticLastNameStr = (NSString *)phoneticLastName;
            NSString *middleNameStr = (NSString *)middleName;
            NSString *suffixNameStr = (NSString *)suffixName;
            NSString *nickNameStr = (NSString *)nickName;
            NSString *jobTitleStr = (NSString *)jobTitle;
            NSString *departmentStr = (NSString *)department;
            NSString *notesStr = (NSString *)notes;
于 2013-06-04T03:18:42.677 回答
0

如果您错误地推送了某个视图控制器而不是另一个视图控制器,您将收到此错误。为了我..

我的代码像.. MainViewController *mainVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"mainView"];

[self.navigationController presentViewController:mainVC 动画:YES 完成:nil];

而不是 .... SWRevealViewController *mainVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"revealView"];

[self.navigationController presentViewController:mainVC 动画:YES 完成:nil];

因此,请确保您正在推动正确的视图控制器。我希望这可以帮助别人。

于 2015-04-09T05:21:50.440 回答