Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Array objects

obaidjawadobaidjawad Posts: 133Registered Users
I'm adding the objects to the array but it is only getting added to the index 0.


- (void)viewDidLoad {
// Implement viewDidLoad if you need to do additional setup after loading the view.
[super viewDidLoad];

ABAddressBookRef addressBook = ABAddressBookCreate(); // create address book reference object
NSArray *abContactArray = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook); // get address book contact array



NSInteger totalContacts =[abContactArray count];

for(NSUInteger loop= 0 ; loop < totalContacts; loop++)
{
ABRecordRef record = (ABRecordRef)[abContactArray objectAtIndex:loop]; // get address book record
// NSLog(@\"%@,%@,%@\",recordIdString,firstNameString,lastNameString);



if(ABRecordGetRecordType(record) == kABPersonType) // this check execute if it is person group
{
ABRecordID recordId = ABRecordGetRecordID(record); // get record id from address book record

recordIdString = [NSString stringWithFormat:@\"%d\",recordId]; // get record id string from record id

firstNameString = (NSString*)ABRecordCopyValue(record,kABPersonFirstNameProperty); // fetch contact first name from address book
lastNameString = (NSString*)ABRecordCopyValue(record,kABPersonLastNameProperty); // fetch contact last name from address book
myArray2 = [NSArray arrayWithObjects:[NSString stringWithFormat:@\"%@\",firstNameString],nil];


}
}
[B] NSLog(@\"Out of loop %@\",[myArray2 objectAtIndex:0]); // Problem description below [/B]
}



NSArray * myArray2 is declared in .h class


Output is :


2012-05-07 12:12:29.772 addressBook[1578:207] Abdullah
2012-05-07 12:12:29.775 addressBook[1578:207] Hussain
2012-05-07 12:12:29.882 addressBook[1578:207] Nasir
2012-05-07 12:12:29.882 addressBook[1578:207] Ghulam
2012-05-07 12:12:29.887 addressBook[1578:207] Wassey
2012-05-07 12:12:29.888 addressBook[1578:207] Listhan
2012-05-07 12:12:29.889 addressBook[1578:207] Mehmood
2012-05-07 12:12:29.896 addressBook[1578:207] Ali

2012-05-07 12:12:29.896 addressBook[1578:207] Out of Loop Ali


I need the same output but it should be on different index's. like for Abdullah it should be on index 0, for Hussain on index 1, and so on.

Problem Description:

Here only one object is displayed and it should because at index 0 the last object is getting added after coming out of the loop, So. That is why want to add the objects on different index's.
Post edited by obaidjawad on

Replies

  • Duncan CDuncan C Posts: 8,014Tutorial Authors, Registered Users
    obaidjawad;430235 said:
    I'm adding the objects to the array but it is only getting added to the index 0.


    - (void)viewDidLoad {
    // Implement viewDidLoad if you need to do additional setup after loading the view.
    [super viewDidLoad];

    ABAddressBookRef addressBook = ABAddressBookCreate(); // create address book reference object
    NSArray *abContactArray = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook); // get address book contact array



    NSInteger totalContacts =[abContactArray count];

    for(NSUInteger loop= 0 ; loop < totalContacts; loop++)
    {
    ABRecordRef record = (ABRecordRef)[abContactArray objectAtIndex:loop]; // get address book record
    // NSLog(@\"%@,%@,%@\",recordIdString,firstNameString,lastNameString);



    if(ABRecordGetRecordType(record) == kABPersonType) // this check execute if it is person group
    {
    ABRecordID recordId = ABRecordGetRecordID(record); // get record id from address book record

    recordIdString = [NSString stringWithFormat:@\"%d\",recordId]; // get record id string from record id

    firstNameString = (NSString*)ABRecordCopyValue(record,kABPersonFirstNameProperty); // fetch contact first name from address book
    lastNameString = (NSString*)ABRecordCopyValue(record,kABPersonLastNameProperty); // fetch contact last name from address book
    myArray2 = [NSArray arrayWithObjects:[NSString stringWithFormat:@\"%@\",firstNameString],nil];


    }
    }
    [B] NSLog(@\"Out of loop %@\",[myArray2 objectAtIndex:0]); // Problem description below [/B]
    }



    NSArray * myArray2 is declared in .h class


    Output is :


    2012-05-07 12:12:29.772 addressBook[1578:207] Abdullah
    2012-05-07 12:12:29.775 addressBook[1578:207] Hussain
    2012-05-07 12:12:29.882 addressBook[1578:207] Nasir
    2012-05-07 12:12:29.882 addressBook[1578:207] Ghulam
    2012-05-07 12:12:29.887 addressBook[1578:207] Wassey
    2012-05-07 12:12:29.888 addressBook[1578:207] Listhan
    2012-05-07 12:12:29.889 addressBook[1578:207] Mehmood
    2012-05-07 12:12:29.896 addressBook[1578:207] Ali

    2012-05-07 12:12:29.896 addressBook[1578:207] Out of Loop Ali


    I need the same output but it should be on different index's. like for Abdullah it should be on index 0, for Hussain on index 1, and so on.

    Problem Description:

    Here only one object is displayed and it should because at index 0 the last object is getting added after coming out of the loop, So. That is why want to add the objects on different index's.


    This line:


    myArray2 = [NSArray arrayWithObjects:[NSString stringWithFormat: @\"%@\",firstNameString],nil];


    Is not doing what you think. The method arrayWithObjects creates a new array that contains only the objects you provide in the call. If myArray2 contained an object before, it gets overwritten with the newly created object. By the time you get to your "outside the loop" statement, your array only contains 1 object.

    You should create a mutable array object before entering the loop, and then add each object to the array with the method addObject:
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • obaidjawadobaidjawad Posts: 133Registered Users
    Duncan C;430255 said:
    This line:


    myArray2 = [NSArray arrayWithObjects:[NSString stringWithFormat: @\"%@\",firstNameString],nil];


    Is not doing what you think. The method arrayWithObjects creates a new array that contains only the objects you provide in the call. If myArray2 contained an object before, it gets overwritten with the newly created object. By the time you get to your "outside the loop" statement, your array only contains 1 object.

    You should create a mutable array object before entering the loop, and then add each object to the array with the method addObject:
    I solved this issue and you are correct. Could you please check this post http://www.iphonedevsdk.com/forum/iphone-sdk-development/103467-phone-numbers-retrieval-issue-xcode.html
Sign In or Register to comment.