要从前面或后面删除项目,您可以使用subarrayWithRange
,即:
NSRange allExceptLast;
allExceptLast.location = 0;
allExceptLast.length = [self.toolbarItems count] - 1;
self.toolbarItems = [self.toolbarItems subarrayWithRange:allExceptLast];
如果要从中间删除对象,可以使用-[NSArray filteredArrayUsingPredicate:]
(可能过于复杂)或蛮力:
NSMutableArray *mutToolbarItems = [NSMutableArray arrayWithArray:self.toolbarItems];
[mutToolbarItems removeObjectAtIndex:<index of object>];
self.toolbarItems = mutToolbarItems;
Note that you shouldn't send removeObjectAtIndex:
to self.toolbarItems
directly (even if you use the above method), since toolbarItems
is exposed as an NSArray
--you'll get a compiler warning, and possibly a crash (since you have no control over whether it will actually be implemented as an NSMutableArray
behind the scenes).