'NSInvalidArgumentException' error when adding data to a UITable?
So this is the set up of my app. In my view did load method, I create an NSMutableArray, and I initialize it with a list of object (using the initWithObjects:
method). This array get's loaded into a table view without any problems. I've also added a "+" button in the navigation bar to add data to my table. My problem is that every time I press this "+" my app freezes and I get the SIGABRT signal. Can someone tell me what I'm doing wrong?
I've retained the array using a property, and I've synthesized it. I'm also releasing it in the dealloc method.
I'm creating the array like this:
NSMutableArray *array = [[NSArray alloc] initWithObjects:@"data 1", @"data 2", @"data 3", nil];
self.myArray = array;
[array release];
I'm creating the "+" button in the nav bar with this:
UIBarButtonItem *addDataButton = [[UIBarButtonItem alloc] initWithTitle:@"+"
style:UIBarButtonItemS开发者_如何学编程tyleBordered
target:self
action:@selector(addData)];
My method for adding data is:
- (void)addData {
[myArray addObject:@"some data"];
[self.tableView reloadData];
}
Also if this helps at all, the error message I get in the console is this:
2011-02-19 13:02:09.987 MyApp[480:307] -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x1bee50
2011-02-19 13:02:10.020 MyApp[480:307] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x1bee50'
You're creating an NSArray
and not an NSMutableArray
and so you can't add objects to it. Change the array initialization to:
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"data 1", @"data 2", @"data 3", nil];
精彩评论