IOS [__NSPlaceholderArray initWithObjects:count:]: 尝试从 objects[0]' 插入 nil 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16365384/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:27:22  来源:igfitidea点击:

IOS [__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'

iosuitableviewnsarraytableview

提问by roxeman3

I am trying to create a dynamic slide menu like facebook.

我正在尝试创建一个像 facebook 这样的动态幻灯片菜单。

I have to get some data from a json request. and display this data in one of the sections of the slide table view. I'm new in objective-c, I also tried with NSMutableArrays and I had an error. In other table views I do the same but without sections. only one MutableArray and I can show the table.

我必须从 json 请求中获取一些数据。并在幻灯片表视图的部分之一中显示此数据。我是objective-c的新手,我也尝试过NSMutableArrays,但出现错误。在其他表格视图中,我也这样做,但没有节。只有一个 MutableArray,我可以显示表格。

I do something like this:

我做这样的事情:

-(void) requestJSONFinishedWithParsedObject:(NSDictionary *)parsedObject{


  NSArray  *projectsObjectArray = [parsedObject objectForKey:@"projects"];

    [self createMainNavigationController];
    self.section1 = [NSArray arrayWithObjects:@"Profile", @"Notifications", @"Exit", nil];
    self.section2 = [NSArray arrayWithObjects:@"Main", nil];


    for (NSDictionary *projectObject in projectsObjectArray)
    {
        Project *newProject = [[Project alloc] init];

        newProject.title= [projectObject objectForKey:@"title"];

        self.section3 = [NSArray arrayWithObject:newProject.title];
    }

    self.menu = [NSArray arrayWithObjects:self.section1, self.section2, self.section3, nil];
    [menuTableView reloadData];
}

I am having this error :

我有这个错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'

回答by JeffN

It is because

这是因为

newProject.title= [projectObject objectForKey:@"title"];

is nil, and you are trying to add it to the array. Check to see what that value is by logging it

为零,并且您正尝试将其添加到数组中。通过记录它来检查该值是什么

NSLog(@"%@", [projectObject objectForKey:@"title"]);