Search code examples
iosiphoneios7nsarray

getting NSArray objects using index numbers


I have a array with 8 objects now and i have another array with 4 index's .I want to get objects from the first array using index values present in the second array.(objective c)

NSArray *arr = @[@"ECE",@"CSE",@"MECH",@"CIVIL",@"AERO",@"IT",@"EEE",@"EM"];

NSArray *indexNumberArray = @[0,2,5,7];

Solution

  • Try this :

    NSArray *arr = @[@"ECE",@"CSE",@"MECH",@"CIVIL",@"AERO",@"IT",@"EEE",@"EM"];
    
        NSArray *indexNumberArray = @[@0,@2,@5,@7];
        NSMutableArray *arrNew = [NSMutableArray new];
        for (NSNumber *index in indexNumberArray) {
           [arrNew addObject:[arr objectAtIndex:[index integerValue]]];
        }
    

    Output

    enter image description here