如何将 NSMutableArray 数组分成两个单独的数组或 objective C、iOS 中的两个可变数组
how to divide NSMutableArray array into two seperate arrays or two mutable arrays in objective C, iOS
我有一个包含 4 个的 nsmutable 数组 objects.no 我想根据条件将该对象分成两个数组(或两个可变数组)。
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
这是一个简单的方法。您可以根据需要进行概括。
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];
您可以仅从 testingArray 添加对象到 OutboundArray 或 InboundArray,即对于第一个条件,只需将索引 0 和 1 用于前两个对象的 OutboundArray,将索引 2 和 3 用于 InboundArray.Just,相应地在其他条件下使用相同的对象。 @Note 在从索引中获取之前首先检查 testingArray 中的对象,否则它将 crash.Always 以小写开头的变量名:)
if(firstCondition)
{
[OutboundArray addObject:[testingArray objectAtIndex:0]];
[OutboundArray addObject:[testingArray objectAtIndex:1]];
[InboundArray addObject:[testingArray objectAtIndex:2]];
[InboundArray addObject:[testingArray objectAtIndex:3]];
}
使用此代码:
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];
}
我有一个包含 4 个的 nsmutable 数组 objects.no 我想根据条件将该对象分成两个数组(或两个可变数组)。
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
这是一个简单的方法。您可以根据需要进行概括。
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];
您可以仅从 testingArray 添加对象到 OutboundArray 或 InboundArray,即对于第一个条件,只需将索引 0 和 1 用于前两个对象的 OutboundArray,将索引 2 和 3 用于 InboundArray.Just,相应地在其他条件下使用相同的对象。 @Note 在从索引中获取之前首先检查 testingArray 中的对象,否则它将 crash.Always 以小写开头的变量名:)
if(firstCondition)
{
[OutboundArray addObject:[testingArray objectAtIndex:0]];
[OutboundArray addObject:[testingArray objectAtIndex:1]];
[InboundArray addObject:[testingArray objectAtIndex:2]];
[InboundArray addObject:[testingArray objectAtIndex:3]];
}
使用此代码:
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];
}