我有一个 NSMutable 数组,其中插入了这样的元素:
[availableSeatsArray addObject:[NSString stringWithString:num_available]];
我想删除数组中值为零或更低的元素。我尝试使用它的 int 值甚至它的字符串值来检查元素的值,但它总是通过 '0' 元素大小写。数组前后的控制台输出如下。
for (int i=0;i<[availableSeatsArray count]; i++) {
if ([[availableSeatsArray objectAtIndex:i] intValue] <= 0 || ([[availableSeatsArray objectAtIndex:i] isEqualToString:@"0"])) {
NSLog(@"Removed index: %d", [[availableSeatsArray objectAtIndex:i] intValue]);
[availableSeatsArray removeObjectAtIndex:i];
}
}
控制台输出:
Available array: (
"-2",
10,
5,
"-5",
0,
10,
10,
)
2012-08-14 11:13:28:002 -[dmbAddReservation viewWillAppear:] [Line 1074] Removed index: -2
2012-08-14 11:13:28:004 -[dmbAddReservation viewWillAppear:] [Line 1074] Removed index: -5
2012-08-14 11:13:28:006 -[dmbAddReservation viewWillAppear:] [Line 1083] Available array: (
10,
5,
0, // I cannot explain why this element was not removed
10,
10,
)