2016-02-25 39 views
0

我有一個nsmutable數組有4個對象。我想根據條件將這些對象分成兩個數組(或兩個可變數組) 。如何將NSMutableArray數組分成兩個單獨的數組或兩個可變數組,在目標C中,iOS

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

所以這個測試nsmutable陣列有四個objects.now我想喜歡這個。

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 
} 

我該怎麼辦this.hope你help.thanx

回答

1

使用此代碼:

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

}

+0

這工作。但請告訴我,如果testingArray計數是2,如何將第一個對象到第一個對象入站數組和第二個對象到入站數組。我得到錯誤。 –

+0

更新了我的答案。 –

0

這裏有一個簡單的方法。您可以根據需要進行概括。

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

下面是第二種避免顯式循環的方法。這需要一個真正的分割,而第一個很容易修改,以允許在數組之間進行任意路由。

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

可以只是從testingArray即添加在OutboundArray或InboundArray對象爲第一條件只是使用索引0和1用於前兩個對象OutboundArray和索引2和3 InboundArray.Just相應地使用在其他條件相同。 @Note在testingArray第一次檢查的對象從索引讀取前否則將crash.Always開始的變量名小case :)

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

} 
+0

向我展示一個code.please –

相關問題