Search code examples
iosobjective-cnsmutablearraynsarray

how to divide NSMutableArray array into two seperate arrays or two mutable arrays in objective C, iOS


I have an nsmutable array with 4 objects.no I want to divide that objects into two arrays(or two mutable arrays) based on a condition.

for  (NSDictionary *final in SectorsArray)
{
    FinalFlightData *ffd = [FinalFlightData new];
    ffd.flightnumber = [final objectForKey:@"FLI_NUM"];
    ffd.airlineCode = [final objectForKey:@"ARL_COD"];

    ffd.departureAirport = [final objectForKey:@"DepartureAirport"];
    ffd.departureDay = [final objectForKey:@"DepartureDay"];
    ffd.departureDate = [final objectForKey:@"DepartureDate"];
    ffd.departureTime = [final objectForKey:@"DepartureTime"];

    ffd.arrivalAirport = [final objectForKey:@"ArrivalAirport"];
    ffd.arrivalDay = [final objectForKey:@"ArrivalDay"];
    ffd.arrivalDate = [final objectForKey:@"ArrivalDate"];
    ffd.arrivalTime = [final objectForKey:@"ArrivalTime"];




    [testingArray addObject:ffd];
}

so this testing nsmutable array has four objects.now I want to like this.

OutboundArray = [NSMutableArray array];
InboundArray = [NSMutableArray array];
NSString *myString;

if([myString isEqualtoString:@"false"])
{
  //in here I want to put first two objects of testingArray into OutboundArray and last two objects of testingArray into InboundArray

}

else if([myString isEqualtoString:@"true"])
{
  //in here I want to put first three objects of testingArray into OutboundArray and last object of testingArray into InboundArray
}
else
{
  //in here I want to put first  object of testingArray into OutboundArray and last three objects of testingArray into InboundArray
}

how can I do this.hope your help.thanx


Solution

  • Use this code :

    if (testingArray.count >= 3)
    {
        if([myString isEqualToString:@"false"])
        {
            //in here I want to put first two objects of testingArray into OutboundArray and last two objects of testingArray into InboundArray
    
            [OutboundArray addObjectsFromArray:[testingArray subarrayWithRange:NSMakeRange(0, 2)]];
            [InboundArray addObjectsFromArray:[testingArray subarrayWithRange:NSMakeRange(testingArray.count - 2, 2)]];
    
        }
        else if([myString isEqualToString:@"true"])
        {
            //in here I want to put first three objects of testingArray into OutboundArray and last object of testingArray into InboundArray
    
            [OutboundArray addObjectsFromArray:[testingArray subarrayWithRange:NSMakeRange(0, 3)]];
            [InboundArray addObject:testingArray.lastObject];
    
        }
        else
        {
            //in here I want to put first  object of testingArray into OutboundArray and last three objects of testingArray into InboundArray
    
            [OutboundArray addObject:testingArray.firstObject];
            [InboundArray addObjectsFromArray:[testingArray subarrayWithRange:NSMakeRange(testingArray.count - 3, 3)]];
        }
    }
    else if (testingArray.count == 2)
    {
        [OutboundArray addObject:testingArray.firstObject];
        [InboundArray addObject:testingArray.lastObject];
    

    }