0
我在陣列5分的對象和要刪除環2,但我在下面的代碼NSArray中沒有刪除對象
NSMutableArray *totalPages = [[NSMutableArray alloc] init];
[totalPages addObject:@"Test1"];
[totalPages addObject:@"Test2"];
[totalPages addObject:@"Test3"];
[totalPages addObject:@"Test4"];
[totalPages addObject:@"Test5"];
int currentPage = 2;
for (int x = 0; x < [totalPages count]; x++) {
//int pageIds = [[totalPages objectAtIndex:x] intValue];
//NSLog(@"%d",pageIds);
NSLog(@"Array Count %d", (int)[totalPages count]);
NSLog(@"Current Page %d", currentPage);
NSLog(@"Current Iterator Value %d", x);
if (x > currentPage) {
[totalPages removeObjectAtIndex:x];
NSLog(@"Array Count %d", (int)[totalPages count]);
NSLog(@"Number of Pages to be removed %d", x);
}
}
小的小問題,當我要刪除「TEST4」和「TEST5」,但我的上述代碼只刪去「TEST5」如果我保持這種邏輯
if (x >= currentPage)
所以它刪除我的「TEST4」和「TEST5」對象,但邏輯失敗時INT當前頁= 0;那麼什麼是推薦的方法來刪除Test4和Test5作爲數組中的對象是動態添加的,當currentPage = 0時;所以Arrays只有一個Object作爲頁面。
[迭代時從NSMutableArray中移除的最佳方法?](http://stackoverflow.com/questions/111866/best-way-to-remove-from-nsmutablearray-while-iterating) – 2014-09-21 18:05:27