我正在尝试将数组的某些索引处的对象添加到新数组中。我找到了一个解决方案,但有更优雅的方法吗?这就是我所拥有的。
NSMutableArray *objectsToAdd = [[NSMutableArray alloc]init];
for (NSIndexPath *indexPath in self.selectedIndexes)
{
for (Object *object in self.unsortedObjects) {
if (indexPath.row == [self.unsortedObjects indexOfObject:object]) {
[objectsToAdd addObject:object];
}
}
}
答案 0 :(得分:0)
我相信self.selectedIndexes
是NSArray
或NSMutableArray
。希望帮助
NSMutableArray *objectsToAdd = [[NSMutableArray alloc]init];
for (NSIndexPath *indexPath in self.selectedIndexes)
{
if (indexPath.row < [self.unsortedObjects count]) {
[objectsToAdd addObject:self.unsortedObjects[indexPath.row]] ;
}
}