Я новичок в программировании на Objective-C, поэтому прошу прощения за неудачный вопрос.
Я пытаюсь написать какое-то простое приложение, которое бы добавляло отдельные числа, хранящиеся в NSMutableArray, в табличное представление.
Вот код инициализации и код для метода addItem ():
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Test App";
self.navigationItem.leftBarButtonItem = self.editButtonItem;
addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addItem)];
self.navigationItem.rightBarButtonItem = addButton;
self.itemArray = [[NSMutableArray alloc] initWithCapacity:100];
}
- (void)addItem {
[itemArray insertObject:@"1" atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
Эта строка
[itemArray insertObject:@"1" atIndex:0];
выдает следующую ошибку:
*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
Почему индекс 0 выходит за границы для пустого массива и что я делаю неправильно ???
UPD
Как указал BP, вставка в массив может не работать дляпустой массив, поэтому я добавил следующую проверку:
if ([itemArray count] == 0) {
[itemArray addObject:@"1"];
} else {
[itemArray insertObject:@"1" atIndex:0];
}
Итак, теперь я получаю то же сообщение об ошибке, но в строке
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
Есть идеи, что с ним может быть не так?