0

I have a class Person with variables name,age,local and about, i added values of person object to a mutable array as shown:

Person *myPerson=[[Person alloc]init];
NSMutableArray *personArray=[[NSMutableArray alloc]init];

myPerson.name=namefield;    //'namefield' retrieved from db
myPerson.age=agefield;      //'agefield' retrieved from db
myPerson.local=locfield;    //'locfield' retrieved from db
myPerson.about=aboutfield;  //'aboutfield' retrieved from db

[personArray addObject:myPerson];

and I have problem when trying to print out the elements using below code;which shows last element repeatedly,

for (int i = 0; i < [personArray count]; i++){
    Person * p = [personArray objectAtIndex:i];
    NSLog(@"name %@",p.name);
    NSLog(@"age %@",p.age);      
}

hope you will be kind to me as I am a new guy to iPhone development. Thanks.

6

4 Answers 4

3

you can write code like this

NSMutableArray *personArray=[[NSMutableArray alloc]init];
for(int i=o;i<noOfRecords;i++)
{
    Person *myPerson=[[Person alloc]init];
    myPerson.name=namefield;    //'namefield' retrieved from db
    myPerson.age=agefield;      //'agefield' retrieved from db
    myPerson.local=locfield;    //'locfield' retrieved from db
    myPerson.about=aboutfield;  //'aboutfield' retrieved from db

    [personArray addObject:myPerson];
    [myPerson release];
}

Then retrieve array this may be helpful because you are fetching only last row

release if ARC is not enabled otherwise no need to release

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

2 Comments

yes that is my point but i come to know later that you also post same
yeah sure ..no issue, things should be clear to him, and that is all that matters :) :) and you provide a nice code showing him cheers
2

Person *myPerson=[[Person alloc]init]; this line, it seems you have written this line outside the for loop where you are adding objects. As such there is only one instance of person class being there represented by all objects of the array. put that line inside the for loop, so that at every iteration a new person object is added to the array.

Comments

2

It looks like you are re-using myPerson. When you add an object to an array, it does not create a copy of it. Instead, it just stores a reference to the object, therefore when you change it later it changes ALL of the references pointing to it.

Change this line:

[personArray addObject:myPerson];

To this line:

[[personArray addObject:myPerson] copy];

in order to add a copy of your person to the array.

Comments

0
Person *myPerson=[[Person alloc]init];
NSMutableArray *personArray=[[NSMutableArray alloc]init];

myPerson.name=namefield;    //'namefield' retrieved from db
myPerson.age=agefield;      //'agefield' retrieved from db
myPerson.local=locfield;    //'locfield' retrieved from db
myPerson.about=aboutfield;  //'aboutfield' retrieved from db

for (int i = 0; i < [personArray count]; i++) 
{
    [personArray addObject:myPerson];
    NSLog(@"name %@",p.name);
}

Try this...

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.