创建包含另一个数组的某些元素的数组的方法是什么?我有代码:
NSArray *columnArray = @[@"o", @"o", @"o", @"o", @"o", @"o", @"o", @"o", @"o", @"o"];
NSMutableArray *board = [[NSMutableArray alloc] initWithCapacity:10];
for (int columnIndex = 0; columnIndex < 10; columnIndex ++)
{
[board addObject:[columnArray mutableCopy]];
}
for (int i=0; i<4; i++)
{
int column1 = arc4random() % 10;//column
int row1 = arc4random() % 10;//row
if ([[[board objectAtIndex:column1] objectAtIndex: row1 ] isEqualToString:@"X" ])
{
column1=arc4random() %10;
row1=arc4random() %10;
}
[[board objectAtIndex:row1] replaceObjectAtIndex:column1 withObject:@"X"];
}
如何将“X”的所有元素放在另一个数组中?
答案 0 :(得分:0)
NSPredicate
和filteredArrayUsingPredicate
:非常简单易用:
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(NSString *object, NSDictionary *bind){
return // your test here, evaluating to a BOOL
}];
NSArray *found = [myArray filteredArrayUsingPredicate:predicate];