ios - NSInvalidArgumentException',reas-[__NSPlaceholderDictionary initWithObjects :forKeys:count:]: attempt to insert nil object from objects[4]'

标签 ios objective-c null

我收到错误 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

这是我的 viewdidload

self.users = [DMZUserData users];
NSLog(@"%@", self.users);

最佳答案

该错误意味着您正试图将 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;
}

现在您可以使用调试器查看 image1image2image3image4 是否正确nil 或为每个添加 NSLog 语句。

请记住,文件名区分大小写,因此请确保您传递给 imageNamed: 的名称与真实文件名完全匹配。还要验证图像的扩展名为 jpeg 而不是 jpg。确保图像被打包到您的资源包中。

关于ios - NSInvalidArgumentException',reas-[__NSPlaceholderDictionary initWithObjects :forKeys:count:]: attempt to insert nil object from objects[4]',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23298615/

相关文章:

iOS 滑动抽屉

ios - 如何在一个 View Controller 中快速将占位符设置为多个 TextView

ios - 将 UIButtons 添加为 subview 时 UIScrollView 不滚动

ios - 触摸单元格时更改 UITableViewCell 的附件类型

旋转屏幕后,Android 微调器变为空

ios - 在我的 iOS 应用程序中包含一个 HTTP(而非 HTTPS)URL 链接,我是否需要禁用应用程序传输安全?

ios - 将结构传递给 OCMock invokeBlockWithArgs 以获得 AVCaptureSession

ios - 带有自定义多边形的box2d崩溃

PHP 认为 null 等于零

mysql - 如何使用 MySQL 将空值插入到已有值的列中