Obj-C:在Array的索引处添加对象

时间:2015-03-16 01:26:29

标签: objective-c arrays

我正在尝试将数组的某些索引处的对象添加到新数组中。我找到了一个解决方案,但有更优雅的方法吗?这就是我所拥有的。

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

1 个答案:

答案 0 :(得分:0)

我相信self.selectedIndexesNSArrayNSMutableArray。希望帮助

NSMutableArray *objectsToAdd = [[NSMutableArray alloc]init];
for (NSIndexPath *indexPath in self.selectedIndexes)
{
    if (indexPath.row < [self.unsortedObjects count]) {
        [objectsToAdd addObject:self.unsortedObjects[indexPath.row]] ;
    }
}