0

HI here is the below function where i am collecting NSString values like firstname and last name from the function and assign in it array

- (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]];
}
}

And also to send data

- (IBAction)sendContacts:(id)sender
{

NSData *textData = [self.messageToSend dataUsingEncoding:NSASCIIStringEncoding];
//send data to all connected devices
[self.session sendDataToAllPeers:textData withDataMode:GKSendDataReliable error:nil];
}

here messageToSend is a NSMutableString

So in the above code i am collecting all firstname and lastname and placing in it array.When i run this in iphone it shows error [__NSArrayM insertObject:atIndex:]: object cannot be nil'

After NSLog(@"Myresult"); it is displaying firstname and lastname twice.so can anyone tel me where i am goin wrong and same way how do i collect phonenumber as wel.what changes i have to do in above code to avoid that error and it displaying only few names not all.

It displays

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::::::
8
  • Where messageToSend is allocated? Commented Jun 3, 2013 at 6:05
  • in ViewDidload method Commented Jun 3, 2013 at 6:06
  • Please check that where you are assigning the first and last name to NSMutableArray is anything nil or not? Plesae put NsLog before to add object to array or use if(firstname){[add object];}if(lastname){[addobject]} before inserting object to array Commented Jun 3, 2013 at 6:11
  • 1
    When you are adding objects to array please check it is nil or not? If it is nil don't add to array otherwise add it to array. Am i clear to you? Commented Jun 3, 2013 at 6:18
  • 1
    [name addObject:firstName]; [name addObject:lastName]; before adding object check the firstname,lastname is not null Commented Jun 3, 2013 at 6:19

3 Answers 3

1

First check if the data you are putting inside the array is nil, if it is just skip it.

something like this:

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

you get the idea.

Sign up to request clarification or add additional context in comments.

4 Comments

ya i got it i put above code but what i would write else statement??if i skip else it works fine rite??
@user2443391 YES you can skip else part
@RahulVyas thank u it works same way how do i retrive Phone number in the above code..what changes i have to make to get phonenumber also
@RahulVyas thank u it works same way how do i retrive Phone number in the above code..what changes i have to make to get phonenumber also
0

To Access most of the things use below code

            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;

Comments

0

If you push some view controller instead of another view controller by mistake you will get this error. For me..

I have coded like.. MainViewController *mainVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"mainView"];

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

instead of .... SWRevealViewController *mainVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"revealView"];

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

So Make sure you are pushing to correct view controller. I hope this helps some one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.