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

Gaj*_*han 5 iphone xcode objective-c ios

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

我试着修复

(1)if([Array containsObject:[NSNull null]])
(2)if([Array objectAtIndex:0
(3)if([Array objectAtIndex:counter] == 0
方法.

我的代码是,

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];
                }
            }
Run Code Online (Sandbox Code Playgroud)

在这里,phoneno是NSMutableArray

Mas*_*oke 12

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

NSMutableArray *phoneno=[[NSMutableArray alloc]init];
Run Code Online (Sandbox Code Playgroud)

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

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


Ale*_*nte 5

您无法插入nil一个NSArray或一个,NSDictionary因此您必须添加测试.

if(str){
   [phoneno addObject:str];
}
Run Code Online (Sandbox Code Playgroud)