0

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

3 Answers 3

1

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];

}

Sign up to request clarification or add additional context in comments.

1 Comment

this works.but please tell me if the testingArray count is 2, how to put first object to onbound array and second object to inbound array.I got error.
1

Here's a simple method. You can generalize as needed.

NSArray *a = @[ @1, @2, @3, @4 ];

NSMutableArray *a1 = [NSMutableArray array];
NSMutableArray *a2 = [NSMutableArray array];

int limit = YES ? 3 : 1;

[a  enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    if (idx < limit) {
        [a1 addObject:obj];
    } else {
        [a2 addObject:obj];
    }
}];

Here's a second method, which avoids an explicit loop. This one requires a true split, whereas the first is easily modified to allow arbitrary routing between arrays.

NSArray *a = @[ @1, @2, @3, @4 ];

NSMutableArray *a1 = [NSMutableArray array];
NSMutableArray *a2 = [NSMutableArray array];

int limit = YES ? 3 : 1;

NSRange a1range = NSMakeRange(0, limit);
NSRange a2range = NSMakeRange(limit, a.count - limit);

a1 = [[a subarrayWithRange:a1range] mutableCopy];
a2 = [[a subarrayWithRange:a2range] mutableCopy];

Comments

0

You can just add objects in OutboundArray or InboundArray from testingArray i.e for first condition just use index 0 and 1 for first two object for OutboundArray and index 2 and 3 for InboundArray.Just use same in other conditions accordingly.@Note first check objects in testingArray before fetching from indexes otherwise it will crash.Always start variable name with small case:)

if(firstCondition)
{
  [OutboundArray addObject:[testingArray objectAtIndex:0]];
  [OutboundArray addObject:[testingArray objectAtIndex:1]];
  [InboundArray addObject:[testingArray objectAtIndex:2]];
  [InboundArray addObject:[testingArray objectAtIndex:3]];

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.