NSMutableArray - How to access

10,509

Solution 1

[[allProject objectAtIndex:0]objectAtIndex:2]

It's basically an array within an array, so you treat it as such.

Solution 2

Are you expecting that [allProject objectAtIndex:0] is an NSString* "Name"? Actually, it is an NSMutableArray, actProject. You're just storing an array inside of an array. This is generally not a good idea.

If you want to add the individual items of actProject to allProject, use -addObjectsFromArray.

 NSMutableArray *allProject= [NSMutableArray array];
 NSMutableArray *actProject = [NSMutableArray array];
 [actProject addObject:@"Name"];
 [actProject addObject:@"Description"];
 [actProject addObject:@"Date"];

 [allProject addObjectsFromArray:actProject];

 NSLog(@"test: %@",[allProject objectAtIndex:0]); //should be @"Name" now.

Solution 3

If I understod right you are looking for:

[[allProject objectAtIndex:0] objectAtIndex:2]

In that way in objective-C you can nest messages to objects. Btw you do not need to cast those object to (NSString *)

Share:
10,509
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I've a little problem: I've created two mutable arrays and added an object of "actProject" to "allProject". Everything works fine but I do not know how to display one single object of allProject (e.g. "Date").

        NSMutableArray *allProject= [[NSMutableArray alloc]initWithObjects: nil];
     NSMutableArray *actProject = [[NSMutableArray alloc]initWithObjects: nil];
     [actProject addObject:(NSString*)@"Name"];
     [actProject addObject:(NSString*)@"Description"];
     [actProject addObject:(NSString*)@"Date"];
    
     [allProject addObject:actProject];
    
     NSLog(@"test: %@",[allProject objectAtIndex:0]);
    

    How to get "Date" only by accessing "allProject"?

    Any ideas?