ios 尝试从对象 [0] 插入 nil 对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11934248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
attempt to insert nil object from objects[0]?
提问by SimplyKiwi
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:
在我的应用程序中,我有一个 UITableView 和一些按钮,用户可以单击这些按钮将数组排序到基于某些 NSDate 或整数的顺序。所以这是我尝试对 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.
所以最初我只是在没有 reloadData 参数的情况下使用了这个方法,因为 UITableView 上的 reloadData 似乎导致了崩溃。
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]'
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“*** -[__NSPlaceholderArray initWithObjects:count:]:尝试从对象 [0]”插入 nil 对象
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?
有任何想法吗?
采纳答案by Tim Dean
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:
至少有一个地方可能会发生这种情况,也许还有其他地方。在获取用户默认值内容的代码中,您不检查nil
. 所以而不是这个:
[self.cellArray addObjectsFromArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"array"]];
you might want to try this:
你可能想试试这个:
NSArray *defaultCellArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"array"];
if (defaultCellArray) {
[self.cellArray addObjectsFromArray:defaultCellArray];
}
It is also possible that the initWithObjects
call that is failing (according to your error message) is nested within another call, perhaps in this call:
initWithObjects
失败的调用(根据您的错误消息)也可能嵌套在另一个调用中,也许在这个调用中:
[self.cellArray sortUsingDescriptors:[NSArray arrayWithObject:descriptor]];
If you look at the full call stack it would tell you if this call is making subsequent calls to initWithObjects
and possibly passing it a nil
. In your case, for example, you would pass in a nil
yo the array you are creating if clickedButton.tag
has 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:
如果您查看完整的调用堆栈,它会告诉您此调用是否正在进行后续调用initWithObjects
并可能将其传递给nil
. 例如,在您的情况下,nil
如果clickedButton.tag
尚未将值设置为 1 或 2 ,您将传入正在创建的数组 yo 。您可能需要添加一个额外的 else 子句来帮助诊断问题:
if (clickedButton.tag == 1) {
descriptor = [NSSortDescriptor sortDescriptorWithKey:@"Date" ascending:NO];
} else if (clickedButton.tag == 2) {
descriptor = [NSSortDescriptor sortDescriptorWithKey:@"Score" ascending:NO];
} else {
NSLog(@"Unexpected error: Can't determine sort descriptor");
}
回答by David H
This is all pretty complicated, but one thing sticks out:
这一切都非常复杂,但有一点很突出:
[[NSUserDefaults standardUserDefaults] setObject:self.cellArray
forKey:@"cellArray"];
I think you should change that to this:
我认为你应该把它改成这样:
NSArray *array = [NSArray arrayWithArray:self.cellArray];
[[NSUserDefaults standardUserDefaults] setObject:array forKey:@"cellArray"];
Secondly, add asserts() after every place where you think you should have an object, i.e.:
其次,在您认为应该拥有对象的每个地方之后添加 asserts() ,即:
UIButton *theButton = ...
assert(theButton);
You also say that the number of rows is double the cellArray count, guess thats intentional.
您还说行数是 cellArray 计数的两倍,您猜这是故意的。