- [__ NSArrayM insertObject:atIndex:]:object不能为nil

时间:2013-09-18 06:01:01

标签: iphone ios objective-c xcode

我收到的错误对象不能为零。我正在使用Web服务,当我使用循环解析它时崩溃并且发送输出-[__NSArrayM insertObject:atIndex:]:对象不能为nil为错误,因为我在NSMutableArray中的空值上有空值我正在尝试解析。

我尝试用

修复

(1)if([Array containsObject:[NSNull null]])
 (2)if([Array objectAtIndex:0
 (3)if([Array objectAtIndex:counter] == 0
methods。

我的代码是,

if([[[node childAtIndex:counter] name] isEqualToString:@"Phone"]){

                if([phoneno containsObject:[NSNull null]]){
                    phone.text = @"-";
                }
                else{
                    NSString *str = [[node childAtIndex:counter] stringValue];
                    [phoneno addObject:str];
                    NSString *myString = [phoneno componentsJoinedByString:@","];
                    [phone setText:myString];
                    phone.text = [NSString stringWithFormat:@"%@",myString];
                }
            }

这里,phoneno是NSMutableArray

5 个答案:

答案 0 :(得分:11)

确保在将任何对象添加到其中之前已初始化名为phoneno的数组...

NSMutableArray *phoneno=[[NSMutableArray alloc]init];

并停止向其添加任何nil对象......

if(str) //or if(str != nil) or if(str.length>0)
{
//add the str into phoneno array.
}
else
{
 //add the @""
}

答案 1 :(得分:5)

您无法将nil插入NSArrayNSDictionary,因此您必须添加测试。

if(str){
   [phoneno addObject:str];
}

答案 2 :(得分:1)

Hello有一些情况是你在数组中插入nil,所以当你尝试获取nil时它会使应用程序崩溃。所以请在插入数组之前检查值。如果value为nil,则在其中插入空白字符串。

if(str ==  nil || [str isKindOfClass:[NSNull null]])
{
[array addObject:@""];
}
else
{
[array addObject:str];
}

答案 3 :(得分:0)

您使用此实用程序方法验证nil // Nil / Null / NULL检查:

-(BOOL)isObjectEmpty:(id)object
{
    return object == nil || ([object respondsToSelector:@selector(length)] && [(NSData *)object length] == 0) || ([object respondsToSelector:@selector(count)] && [(NSArray *)object count] == 0);
}

答案 4 :(得分:0)

NSString *str = [[node childAtIndex:counter] stringValue];
if(str.length>0)
{
   [phoneno addObject:str];
}
相关问题