对象不能为零错误

时间:2012-01-10 00:55:05

标签: objective-c xcode nsmutablearray

当我尝试向dataArray添加新对象时出现错误。我就是这样定义dataArray

-(NSMutableArray *)dataArray{
    if (!_dataArray){   
        _dataArray = [[NSMutableArray alloc] initWithObjects:
                      [NSMutableArray arrayWithObjects:
                       [NSMutableArray array],
                       [NSMutableArray array],
                       [NSMutableArray array],
                       nil],
                      [NSMutableArray arrayWithObjects:
                       [NSMutableArray array],
                       [NSMutableArray array],
                       [NSMutableArray array],
                       nil], 
                      nil];
    }
    return _dataArray;
}

这是我尝试将对象添加到dataArray的地方:

[[[self.dataArray objectAtIndex:0] objectAtIndex:0] addObject:[NSString stringWithFormat:@"%@ %@",[attributeDict objectForKey:@"material"], [attributeDict objectForKey:@"tool"]]];
[[[self.dataArray objectAtIndex:0] objectAtIndex:1] addObject:[attributeDict objectForKey:@"level"]];
[[[self.dataArray objectAtIndex:0] objectAtIndex:2] addObject:[attributeDict objectForKey:@"probability"]];

以下是我收到的错误:*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

为什么我会收到此错误以及如何解决?

3 个答案:

答案 0 :(得分:4)

您对addObject:的论证可能是nil

测试它:

assert(attributeDict);
assert([attributeDict objectForKey:@"level"]);
assert([attributeDict objectForKey:@"probability"]);

答案 1 :(得分:0)

您尝试插入的其中一个值是nil,这意味着attributeDict不包含“级别”或“概率”键。该字符串不应该是nil,即使它提供了nil参数,它们只显示为(nil)

答案 2 :(得分:-1)

我修好了。我拼错了一个变量名。