我有一個包含NSIndexPath的數組,我想刪除所有具有相同IndexPath.Row的對象。我目前的代碼有一些問題,並不是所有具有相同行的對象都被刪除。 我的代碼是:Objective-C從包含索引路徑的NSArray中刪除對象
rowValue=(int)btn.tag;
for (int i=0; i<[SingletonClass singleton].arraySubMenuItems.count; i++)
{
NSIndexPath * Path = [[SingletonClass singleton].arraySubMenuItems objectAtIndex:i];
int section = (int) Path.section;
if (section == rowValue)
{
NSIndexPath *indexPath = [[SingletonClass singleton].arraySubMenuItems objectAtIndex:i];
[[SingletonClass singleton].arraySubMenuItems removeObjectAtIndex:i];
}
}
您正在迭代並同時修改您的數組(尤其是刪除項目)。 – Larme
是的,我知道。我該怎麼辦? –
你可以使用每一個,然後刪除該對象[[SingletonClass singleton] .arraySubMenuItems removeObject:indexPath]; –