0

Can anybody help me to tell me how can access to objects in NSMutableArray.

This is how the creation of the objects is been made:

ViewController:

Students *studens =[[Students alloc] initWithDictionary:data]; //where data is NSDictionary
[self.studentData addObject:studens]; //where studentData is a NSMutableArray

in the Students is a NSObject class. in the Students class:

-(id)initWithDictionary:(NSDictionary *)studenDictionary
    self = [super init];
    if (self)
    {
        self.studentName= [stateDictionary objectForKey:@"name"];
        self.studenLastName= [stateDictionary objectForKey:@"lastName"];
        self.StundenAddress= [stateDictionary objectForKey:@"address"];
    }
    return self;
}

if I nslog the nsmutablearray I'll get this output:

"<studens: 0x756b970>",
"<studens: 0xf46f9a0>"

my question for example how can I access the information of the student name John ?

1
  • 2
    Suggestion - rename your Students class to Student since each instance represents a single student.
    – rmaddy
    Commented Jul 2, 2013 at 5:54

5 Answers 5

3

Implement the description method in your Students class:

- (NSString *)description {
    return [NSString stringWithFormat:@"Students: name: %@, lastName: %@, address: %@", self.studentName, self.studentLastNAme, self.studentAddress];
}

Now when you log the array, you will see useful information about each Students object in the array.

2

Use This to access:

for(Students *studens in self.studentData)
{
   NSLog(@"Name = %@",students.studentName);
   NSLog(@"LastName = %@",students.studenLastName);
   NSLog(@"Address = %@",students.StundenAddress);
   //or
   NSLog(@"%@",students);//if you have define description method in Students class...
}

If you want to print all local variables value all together as print for object you have to write description method in your Students class Like:

copy and paste this to your Students class...

-(NSString *) description
{
   return [NSString stringWithFormat@"Name = %@ \n Last Name= %@  \n Address = %@",self.studentName,self.studenLastName,self.StundenAddress];
}

Now if you NSLog(@"%@",students); it will show you all info.

0

Please have a look into NSPredicate. It may solve your problem. NSPredicates are queries which can be run on objects and are very helpful in functionalities like searching a particular object from array.

0

Try this

Students *studenobj =[self.studentData objectAtIndex:0];
NSLog(@"%@",studenobj. studentName)
0
if I nslog the nsmutablearray I'll get this output:
"<studens: 0x756b970>",
"<studens: 0xf46f9a0>"

As Your trying to Print Your Student Class Object that's why this you need to access the instance variables for your objects like this.

for(int i=0; i<[self.studentDataArray count]; i++)
{
    Students *studenobject =[self.studentDataArray objectAtIndex:i];
    NSLog(@"Name of Student = %@",studenobject.studentName);
    NSLog(@"Last Name of Student = %@",studenobject.studenLastName);
    NSLog(@"Address of Student = %@",studenobject.StundenAddress);

}

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.