通过比较特定元素来过滤 nsarray
Filter nsarray by comparing specific element
我在 Objective-C,Xcode 8.3,OSX 不是 iOS。
我有一个像这样的 NSMutableArrays
NSMutableArray* A = [NSMutableArray arrayWithObjects:@"one string", @"another string", @"third string", nil];
NSMutableArray* B = [NSMutableArray arrayWithObjects:@"one string", @"second string", nil];
NSMutableArray* C = [NSMutableArray arrayWithObjects:@"one string", @"second string", @"third string", nil];
所有数组都嵌套在另一个数组中
NSMutableArray allArrays = [NSMutableArray arrayWithObjects: A,B,C,nil];
我想过滤所有数组,这样如果最后一个元素(无论有多少项)等于 "third string",那么除了第一个
之外,该数组的所有元素都会被删除
上面例子的结果应该是:
@[
@[@"one string"], // A
@[@"one string", @"second string"], // B
@[@"one string"] // C
]
我已经通过迭代和比较得到了一个有效的解决方案,但必须有更有效的方法!
感谢任何帮助。
NSMutableArray* A = [NSMutableArray arrayWithObjects:@"one string", @"another string", @"third string", nil];
NSMutableArray* B = [NSMutableArray arrayWithObjects:@"one string", @"second string", nil];
NSMutableArray* C = [NSMutableArray arrayWithObjects:@"one string", @"second string", @"third string", nil];
NSMutableArray *allArrays = [NSMutableArray arrayWithObjects: A,B,C,nil];
for (NSMutableArray *innerArray in allArrays)
{
//check that inner is not empty
if (innerArray.count > 0)
{
//Compare the last object
if ([[innerArray lastObject] isEqualToString:@"third string"])
{
//copy first string
NSString *firstString = [innerArray firstObject];
//remove all items from array
[innerArray removeAllObjects];
//Now insert copied string into empty array
[innerArray addObject:firstString];
}
}
}
NSLog(@"%@",allArrays);
Mahesh 答案的简化版:
// enumerate the outer array
[allArrays enumerateObjectsUsingBlock:^(NSMutableArray *innerArray, NSUInteger idx, BOOL *stop){
// test last object
if ([innerArray.lastObject isEqualToString:@"third string"]) {
// remove objects after the first object
[innerArray removeObjectsInRange:NSMakeRange(1, innerArray.count - 1)];
}
}];
我在 Objective-C,Xcode 8.3,OSX 不是 iOS。
我有一个像这样的 NSMutableArrays
NSMutableArray* A = [NSMutableArray arrayWithObjects:@"one string", @"another string", @"third string", nil];
NSMutableArray* B = [NSMutableArray arrayWithObjects:@"one string", @"second string", nil];
NSMutableArray* C = [NSMutableArray arrayWithObjects:@"one string", @"second string", @"third string", nil];
所有数组都嵌套在另一个数组中
NSMutableArray allArrays = [NSMutableArray arrayWithObjects: A,B,C,nil];
我想过滤所有数组,这样如果最后一个元素(无论有多少项)等于 "third string",那么除了第一个
之外,该数组的所有元素都会被删除上面例子的结果应该是:
@[
@[@"one string"], // A
@[@"one string", @"second string"], // B
@[@"one string"] // C
]
我已经通过迭代和比较得到了一个有效的解决方案,但必须有更有效的方法!
感谢任何帮助。
NSMutableArray* A = [NSMutableArray arrayWithObjects:@"one string", @"another string", @"third string", nil];
NSMutableArray* B = [NSMutableArray arrayWithObjects:@"one string", @"second string", nil];
NSMutableArray* C = [NSMutableArray arrayWithObjects:@"one string", @"second string", @"third string", nil];
NSMutableArray *allArrays = [NSMutableArray arrayWithObjects: A,B,C,nil];
for (NSMutableArray *innerArray in allArrays)
{
//check that inner is not empty
if (innerArray.count > 0)
{
//Compare the last object
if ([[innerArray lastObject] isEqualToString:@"third string"])
{
//copy first string
NSString *firstString = [innerArray firstObject];
//remove all items from array
[innerArray removeAllObjects];
//Now insert copied string into empty array
[innerArray addObject:firstString];
}
}
}
NSLog(@"%@",allArrays);
Mahesh 答案的简化版:
// enumerate the outer array
[allArrays enumerateObjectsUsingBlock:^(NSMutableArray *innerArray, NSUInteger idx, BOOL *stop){
// test last object
if ([innerArray.lastObject isEqualToString:@"third string"]) {
// remove objects after the first object
[innerArray removeObjectsInRange:NSMakeRange(1, innerArray.count - 1)];
}
}];