n.e*_*ind 0 iphone cocoa-touch objective-c nsmutablearray
我有一个NSMutable数组,并试图找到该数组中最后一个对象的索引号.我尝试过这个,但感觉很麻烦:
int currentCount = [[[self.myLibrary objectAtIndex:currentNoteBookNumber] tabColours] count];
NSLog(@"Number of tab colours total: %i", currentCount);
NSLog(@"Index number of last object: %i", currentCount-1);
Run Code Online (Sandbox Code Playgroud)
还有另一种方法吗?我的问题的上下文是我需要确定最后一个对象才能更改它:
replaceObjectAtIndex:[last object] withObject: ...
Run Code Online (Sandbox Code Playgroud)
谢谢!
aro*_*oth 12
如果你需要索引,那就是这样做的方法(int lastIndex = [array count] - 1;).如果您只想用不同的对象替换最后一个对象,则可以执行以下操作:
[array removeLastObject];
[array addObject: newLastObject];
Run Code Online (Sandbox Code Playgroud)