xcode -[__NSPlaceholderArray initWithObjects:count:]: 尝试从 objects[0]' 插入 nil 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13319843/
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
-[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'
提问by khaleel
I am working with UITableview i am displaying image, title,date and location name on tableview cell.When i click on tableview cell it will navigate to details view.in continuous process tableview to detail view and vice versa.After some navigations my application getting crash when i click on my UITableview cell.
我正在使用 UITableview 我在 tableview 单元格上显示图像、标题、日期和位置名称。当我单击 tableview 单元格时,它将导航到详细信息视图。在连续处理 tableview 到详细信息视图中,反之亦然。经过一些导航,我的应用程序得到当我点击我的 UITableview 单元格时崩溃。
回答by justin
NSArray
is designed to hold objects for every element -- nil
is never a valid value at any NSArray
index.
NSArray
旨在为每个元素保存对象——nil
在任何NSArray
索引处都不是有效值。
The error suggests your program is creating an array with a non-zero length, and that the first element is nil.
该错误表明您的程序正在创建一个长度非零的数组,并且第一个元素为零。
Hunch: You have declared a literal, something equivalent to:
预感:您已经声明了一个字面量,相当于:
NSNumber * n = nil;
NSArray * a = @[n]; // << runtime error creating this array
Of course, your program could instead be calling -[NSArray initWithObjects:count:]
directly.
当然,您的程序也可以-[NSArray initWithObjects:count:]
直接调用。
回答by Bruno Garelli
1- Define a safe macro
#define $safeArgs(...) [NSOrderedSet orderedSetWithObjects:__VA_ARGS__, nil]
1- 定义一个安全的宏
#define $safeArgs(...) [NSOrderedSet orderedSetWithObjects:__VA_ARGS__, nil]
2- Then use
NSArray *array = [$safeArgs("objectsToSanitize") allObjects];
2-然后使用
NSArray *array = [$safeArgs("objectsToSanitize") allObjects];
This will use a set to cleanse your nil objects and then put them into an array
这将使用一个集合来清理您的 nil 对象,然后将它们放入一个数组中