Search code examples
iosobjective-cnsarray

How to select specific index in NSMutableArray and stored in nsarray


I have developed simple app that stores name and id of student into NSMutableArray by using NSDictionary and I want to get names only after stored in NSMutableArray and than store into NSArray

Here my simple code

NSDictionary *student1 = [NSDictionary dictionaryWithObjectsAndKeys:
                          @"key1", @"id",
                          @"Joe", @"name",
                          nil];

NSMutableArray *array = [NSMutableArray arrayWithObjects:student1, nil];

please help me how to solve this.


Solution

  • Try this:

    NSDictionary *student1 = [NSDictionary dictionaryWithObjectsAndKeys:
                              @"key1", @"id",
                              @"Joe", @"name",
                              nil];
    
    NSMutableArray *array = [NSMutableArray arrayWithObjects:student1, nil];
    
    NSMutableArray *mArr = [[NSMutableArray alloc]init];
    for (NSDictionary *student in array) {
        [mArr addObject:[student objectForKey:@"name"]];
    }
    NSArray *arr = mArr;
    NSLog(@"%@",arr);    //OUTPUT: (Joe)