NSArrayM insertObject:atIndex:]:object不能为nil

时间:2013-06-03 06:03:27

标签: ios objective-c nsmutablearray

HI这是以下函数,我从函数中收集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");之后它会显示名字和姓氏两次。所以任何人都可以告诉我我在哪里错了,同样如何收集电话号码。我必须在上面的代码中做些什么改变以避免这种情况错误,它只显示少数名称。

显示

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::::::

3 个答案:

答案 0 :(得分:1)

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

类似的东西:

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

你明白了。

答案 1 :(得分: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;

答案 2 :(得分:0)

如果您错误地推送某个视图控制器而不是另一个视图控制器,则会出现此错误。对我来说..

我已编码为.. MainViewController * mainVC = [[UIStoryboard storyboardWithName:@&#34; Main&#34;束:无] instantiateViewControllerWithIdentifier:@&#34; MAINVIEW&#34;];

[self.navigationController presentViewController:mainVC animated:YES completion:nil];

而不是.... SWRevealViewController * mainVC = [[UIStoryboard storyboardWithName:@&#34; Main&#34;束:无] instantiateViewControllerWithIdentifier:@&#34; revealView&#34;];

[self.navigationController presentViewController:mainVC animated:YES completion:nil];

因此,请确保您正在推动正确的视图控制器。我希望这对某人有所帮助。