Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

I am trying to add and subsequently read objects in an array

I have created this IOS code to load data from my address book into an array. When I try to read the data back and display it, the only displayed data is that of that of the last address added (and many copies of it). I'm pretty sure the problem is an easy fix, but I can't see why this doesn't work:



CFArrayRef _allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );

CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );


NSMutableArray *masters = [[NSMutableArrayalloc] init];

PVMaster *person = [[PVMasteralloc] init];

for ( int i = 0; i < nPeople; i++ )

{

ABRecordRef ref = CFArrayGetValueAtIndex(_allPeople, i );

person._firstName = (__bridge_transferNSString*)ABRecordCopyValue(ref, kABPersonFirstNameProperty);

person._lastName = (__bridge_transferNSString*)ABRecordCopyValue(ref,kABPersonLastNameProperty);

[masters addObject: person];

NSLog(@"person %d %@ %@ [%@]\n",i,person._firstName,person._lastName);

}

for ( int i = 0; (i < [masters count]-1); i++ ){

person = [masters objectAtIndex:i];

NSLog(@"person %d %@ %@ \n",i,person._firstName,person._lastName);

}

I would appreciate suggestions on how to get this code to work.

Mac mini, iOS 8.1.3

Posted on Mar 2, 2015 1:28 AM

Reply
Question marked as Best reply

Posted on Mar 2, 2015 8:17 AM

Containers such as a mutable array contain references to the object in a cell not the object itself. In this case you need to create a new PVMaster object to hold each name from the address book and add that new PVMaster object to the array.


Move

PVMaster *person = [[PVMasteralloc] init];

into the first for loop.


for ( int i = 0; i < nPeople; i++ )

{

PVMasteR *person = [[PVMasteralloc] init];

ABRecordRef ref = CFArrayGetValueAtIndex(_allPeople, i );

person._firstName = (__bridge_transferNSString*)ABRecordCopyValue(ref, kABPersonFirstNameProperty);

person._lastName = (__bridge_transferNSString*)ABRecordCopyValue(ref,kABPersonLastNameProperty);

[masters addObject: person];

NSLog(@"person %d %@ %@ [%@]\n",i,person._firstName,person._lastName);

}

2 replies
Question marked as Best reply

Mar 2, 2015 8:17 AM in response to le mistral

Containers such as a mutable array contain references to the object in a cell not the object itself. In this case you need to create a new PVMaster object to hold each name from the address book and add that new PVMaster object to the array.


Move

PVMaster *person = [[PVMasteralloc] init];

into the first for loop.


for ( int i = 0; i < nPeople; i++ )

{

PVMasteR *person = [[PVMasteralloc] init];

ABRecordRef ref = CFArrayGetValueAtIndex(_allPeople, i );

person._firstName = (__bridge_transferNSString*)ABRecordCopyValue(ref, kABPersonFirstNameProperty);

person._lastName = (__bridge_transferNSString*)ABRecordCopyValue(ref,kABPersonLastNameProperty);

[masters addObject: person];

NSLog(@"person %d %@ %@ [%@]\n",i,person._firstName,person._lastName);

}

I am trying to add and subsequently read objects in an array

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.