ios NSInvalidArgumentException', reas-[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: 尝试从对象中插入 nil 对象 [4]'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23298615/
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
NSInvalidArgumentException', reas-[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[4]'
提问by zramled
i got error NSInvalidArgumentException this is my model class
我收到错误 NSInvalidArgumentException 这是我的模型类
+(NSArray *)users
{
NSDictionary *user1 = @{@"username" : @"master photographer", @"email" : @"[email protected]", @"password" : @"drowssap", @"age" : @24, @"profilePicture" : [UIImage imageNamed:@"person1.jpeg"]};
NSDictionary *user2 = @{@"username" : @"Lots of tots", @"email" : @"[email protected]", @"password" : @"icecreamrocks", @"age" : @65, @"profilePicture" : [UIImage imageNamed:@"person2.jpeg"]};
NSDictionary *user3 = @{@"username" : @"iTechie", @"email" : @"[email protected]", @"password" : @"infiniteloop", @"age" : @30, @"profilePicture" : [UIImage imageNamed:@"person3.jpeg"]};
NSDictionary *user4 = @{@"username" : @"Royal", @"email" : @"[email protected]", @"password" : @"IGotAPalace", @"age" : @0, @"profilePicture" : [UIImage imageNamed:@"person4.jpeg"]};
NSArray *userArray = @[user1, user2, user3, user4];
return userArray;
}
@end
and this is my viewdidload
这是我的viewdidload
self.users = [DMZUserData users];
NSLog(@"%@", self.users);
回答by rmaddy
The error means you are trying to put nil
in the dictionary (which is not allowed). Since you are building the dictionaries with string literals those can't be nil
. This means the problem is with one or more of your images.
该错误意味着您正在尝试放入nil
字典(这是不允许的)。由于您正在使用字符串文字构建字典,因此它们不能是nil
. 这意味着问题出在您的一张或多张图片上。
Try this to help find the problem:
试试这个来帮助找到问题:
+(NSArray *)users
{
UIImage *image1 = [UIImage imageNamed:@"person1.jpeg"];
UIImage *image2 = [UIImage imageNamed:@"person2.jpeg"];
UIImage *image3 = [UIImage imageNamed:@"person3.jpeg"];
UIImage *image4 = [UIImage imageNamed:@"person4.jpeg"];
NSDictionary *user1 = @{@"username" : @"master photographer", @"email" : @"[email protected]", @"password" : @"drowssap", @"age" : @24, @"profilePicture" : image1 };
NSDictionary *user2 = @{@"username" : @"Lots of tots", @"email" : @"[email protected]", @"password" : @"icecreamrocks", @"age" : @65, @"profilePicture" : image2 };
NSDictionary *user3 = @{@"username" : @"iTechie", @"email" : @"[email protected]", @"password" : @"infiniteloop", @"age" : @30, @"profilePicture" : image3 };
NSDictionary *user4 = @{@"username" : @"Royal", @"email" : @"[email protected]", @"password" : @"IGotAPalace", @"age" : @0, @"profilePicture" : image4 };
NSArray *userArray = @[user1, user2, user3, user4];
return userArray;
}
Now you can either use the debugger and see if image1
, image2
, image3
, or image4
is nil
or add NSLog
statements for each.
现在,您可以使用调试器查看 if image1
、image2
、image3
、 or image4
isnil
或NSLog
为每个添加语句。
Keep in mind that filenames are case sensitive so be sure the name you pass to imageNamed:
exactly matches the real filename. Also verify the images have the extension jpeg
and not jpg
. Make sure the images are being packaged in your resource bundle.
请记住,文件名区分大小写,因此请确保传递给的名称imageNamed:
与真实文件名完全匹配。还要验证图像具有扩展名jpeg
而不是jpg
. 确保图像被打包在您的资源包中。
回答by Barbara R
One of the UIImages you're trying to create are nil. Please add the following code as the first line of the method +(NSArray *)users
您尝试创建的 UIImages 之一为零。请添加以下代码作为方法的第一行 +(NSArray *)users
NSAssert([UIImage imageNamed:@"person1.jpeg"] != nil, @"Person 1 image does not exist");
NSAssert([UIImage imageNamed:@"person2.jpeg"] != nil, @"Person 2 image does not exist");
NSAssert([UIImage imageNamed:@"person3.jpeg"] != nil, @"Person 3 image does not exist");
NSAssert([UIImage imageNamed:@"person4.jpeg"] != nil, @"Person 4 image does not exist");
That snippet of code will print on console what of the images does not exist. Run please on DEBUG and let's see what the result of the assertions is.
该代码片段将在控制台上打印哪些图像不存在。请在 DEBUG 上运行,让我们看看断言的结果是什么。
Hope this helps!
希望这可以帮助!
回答by Mike97
The better cure probably is to make the "NSDictionary" like this:
更好的治疗方法可能是像这样制作“NSDictionary”:
NSDictionary *user1 = [NSDictionary dictionaryWithObjectsAndKeys: my obj & key here, nil];
回答by SuperGuyAbe
"attempt to insert nil object from objects[4]" means that either the key or value at index [4] is nil.
“尝试从对象 [4] 插入 nil 对象”表示索引 [4] 处的键或值为零。
So as the others have said it would be one of your images since the key at index [4] is a string literal.
因此,正如其他人所说,这将是您的图像之一,因为索引 [4] 处的键是字符串文字。
回答by Chuck
It means that one of the values you're trying to put in that dictionary is nil, most likely one of the pictures. You might try assigning the images to variables and inspecting them to see if this is the case.
这意味着您尝试放入该字典的值之一为零,很可能是图片之一。您可以尝试将图像分配给变量并检查它们以查看是否是这种情况。