NSInvalidArgumentException',reas - [__ NSPlaceholderDictionary initWithObjects:forKeys:count:]:尝试从对象插入nil对象[4]'

zra*_*led 27 null objective-c ios

我收到错误NSInvalidArgumentException这是我的模型类

+(NSArray *)users
{
    NSDictionary *user1 = @{@"username" : @"master photographer", @"email" : @"worldtravel@me.com", @"password" : @"drowssap", @"age" : @24, @"profilePicture" : [UIImage imageNamed:@"person1.jpeg"]};
    NSDictionary *user2 = @{@"username" : @"Lots of tots", @"email" : @"otterskips@me.com", @"password" : @"icecreamrocks", @"age" : @65, @"profilePicture" : [UIImage imageNamed:@"person2.jpeg"]};
    NSDictionary *user3 = @{@"username" : @"iTechie", @"email" : @"theRealApple@me.com", @"password" : @"infiniteloop", @"age" : @30, @"profilePicture" : [UIImage imageNamed:@"person3.jpeg"]};
    NSDictionary *user4 = @{@"username" : @"Royal", @"email" : @"king@me.com", @"password" : @"IGotAPalace", @"age" : @0, @"profilePicture" : [UIImage imageNamed:@"person4.jpeg"]};

    NSArray *userArray = @[user1, user2, user3, user4];
    return userArray;
}

@end
Run Code Online (Sandbox Code Playgroud)

这是我的viewdidload

self.users = [DMZUserData users];
NSLog(@"%@", self.users);
Run Code Online (Sandbox Code Playgroud)

rma*_*ddy 27

该错误意味着您正在尝试放入nil字典(不允许).由于您使用字符串文字构建字典,因此不能这样nil.这意味着您的一个或多个图像存在问题.

试试这个以帮助找到问题:

+(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" : @"worldtravel@me.com", @"password" : @"drowssap", @"age" : @24, @"profilePicture" : image1 };
    NSDictionary *user2 = @{@"username" : @"Lots of tots", @"email" : @"otterskips@me.com", @"password" : @"icecreamrocks", @"age" : @65, @"profilePicture" : image2 };
    NSDictionary *user3 = @{@"username" : @"iTechie", @"email" : @"theRealApple@me.com", @"password" : @"infiniteloop", @"age" : @30, @"profilePicture" : image3 };
    NSDictionary *user4 = @{@"username" : @"Royal", @"email" : @"king@me.com", @"password" : @"IGotAPalace", @"age" : @0, @"profilePicture" : image4 };

    NSArray *userArray = @[user1, user2, user3, user4];
    return userArray;
}
Run Code Online (Sandbox Code Playgroud)

现在,你可以使用调试器,看看image1,image2,image3,或者image4nil或添加NSLog每个语句.

请记住,文件名区分大小写,因此请确保传递的名称imageNamed:与实际文件名完全匹配.也验证了图像的扩展jpeg,而不是jpg.确保图像正在资源包中打包.


Bar*_*a R 5

您尝试创建的UIImage之一为nil。请添加以下代码作为+(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");
Run Code Online (Sandbox Code Playgroud)

该代码段将在控制台上打印不存在的图像。请在DEBUG上运行,让我们看看断言的结果是什么。

希望这可以帮助!