In my application I have a UITableView and some buttons that the user can click to sort the array to the order based upon some NSDate’s or ints. So this is my method to try to sort my UITableView:
- (void)sortStats:(id)sender reloadData:(BOOL)dataBool {
NSSortDescriptor *descriptor = nil;
UIButton *clickedButton = (UIButton*)sender;
if (clickedButton.tag == 1) {
descriptor = [NSSortDescriptor sortDescriptorWithKey:@"Date" ascending:NO];
}
[self.cellArray sortUsingDescriptors:[NSArray arrayWithObject:descriptor]];
[[NSUserDefaults standardUserDefaults] setObject:self.cellArray forKey:@"cellArray"];
if (dataBool) {
[ivstatstableView reloadData];
}
[ivstatstableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
}
So originally I just had this method without the reloadData parameter because it seemed that reloadData on the UITableView was causing the crash.
This is what the console crash log is:
Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]’
Anyway is there any code here that would be causing this crash? I really want to get this functionality working in my app and I know I am close to fixing it but I am just not sure whats causing this issue.
Any ideas?
There is at least one place where this could be happening, and perhaps others. In the code where you get the contents of the user defaults, you don’t check for
nil. So instead of this:you might want to try this:
It is also possible that the
initWithObjectscall that is failing (according to your error message) is nested within another call, perhaps in this call:If you look at the full call stack it would tell you if this call is making subsequent calls to
initWithObjectsand possibly passing it anil. In your case, for example, you would pass in anilyo the array you are creating ifclickedButton.taghas not yet been set to a value of 1 or 2. You might want to add an additional else clause to help diagnose the problem: