ios -[__NSArrayM insertObject:atIndex:]: 对象不能为零
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18864901/
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
-[__NSArrayM insertObject:atIndex:]: object cannot be nil
提问by Gajendra K Chauhan
I am getting error object cannot be nil. I am using web service, when I parse it using loop it crashed and send output -[__NSArrayM insertObject:atIndex:]:
object cannot be nil as error, because I have null value on blank value in NSMutableArray
which I am trying to parse.
我收到错误对象不能为零。我正在使用 Web 服务,当我使用循环解析它时,它崩溃并发送输出-[__NSArrayM insertObject:atIndex:]:
对象不能为零作为错误,因为我在NSMutableArray
尝试解析的空白值上有空值。
I tried to fix with these methods:
我试图用这些方法修复:
(1) if([Array containsObject:[NSNull null]])
(2) if([Array objectAtIndex:0
(3) if([Array objectAtIndex:counter] == 0**
My code is,
我的代码是,
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];
}
}
Here, phoneno is NSMutableArray
这里,phoneno 是 NSMutableArray
回答by Master Stroke
Make sure you have initialized your array called phoneno before adding any object into it...
在向其中添加任何对象之前,请确保已初始化名为 phoneno 的数组...
NSMutableArray *phoneno=[[NSMutableArray alloc]init];
and stop adding any nil object into it...
并停止向其中添加任何 nil 对象...
if(str) //or if(str != nil) or if(str.length>0)
{
//add the str into phoneno array.
}
else
{
//add the @""
}
回答by Alex Terente
You can not insert nil
in to an NSArray
or a NSDictionary
so you have to add a test.
您不能插入nil
到 anNSArray
或 a 中,NSDictionary
因此您必须添加一个测试。
if(str){
[phoneno addObject:str];
}
回答by Nirmalsinh
There is some case in that you are inserting nil in your array, so when you try to get that value it will crash the application. So please check the value before inserting it into the array. For example, if the value is nil then you could insert blank string into the array instead.
在某些情况下,您在数组中插入 nil,因此当您尝试获取该值时,它会使应用程序崩溃。因此,请在将其插入数组之前检查该值。例如,如果值为 nil,那么您可以将空白字符串插入数组中。
if (str == nil || [str isKindOfClass:[NSNull null]])
{
[array addObject:@""];
}
else
{
[array addObject:str];
}
回答by thatzprem
You this utility method to validate the nil//Nil/Null/NULL checks:
您可以使用此实用程序方法来验证 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);
}
回答by tania_S
NSString *str = [[node childAtIndex:counter] stringValue];
if(str.length>0)
{
[phoneno addObject:str];
}