简体   繁体   中英

iOS:how can query the objects in nsmutablearray objects

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 ?

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.

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.

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.

Try this

Students *studenobj =[self.studentData objectAtIndex:0];
NSLog(@"%@",studenobj. studentName)
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);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM