[英]Check if contain Null
When I print the following I get (null)
当我打印以下内容时,我得到(null)
NSLog(@"%@", [[responseObject objectAtIndex:i] objectForKey:@"child"]);
Now I want to do a validation to check if it returns (null)
. 现在,我要进行验证以检查它是否返回(null)
。 How am I supposed to do this? 我应该怎么做?
I tried the following but it doesn't work: 我尝试了以下操作,但不起作用:
1. 1。
if (![[[responseObject objectAtIndex:i] objectForKey:@"child"] isEqualToString:@"(null)"]) {
}
2. 2。
if (![[[responseObject objectAtIndex:i] objectForKey:@"child"] isEqual:@"(null)"]) {
}
"null" isn't just a NSString
. “ null”不仅是NSString
。 You should do some research into the concept of a null object. 您应该对空对象的概念进行一些研究。
What you're looking for can be written like this: 您要寻找的内容可以这样写:
if (![[responseObject objectAtIndex:i] objectForKey:@"child"]) {
//key "child" not in dictionary
}
(null)
is the representation of nil
displayed by NSLog
. (null)
是NSLog
显示的nil
表示。
You can write the following: 您可以编写以下内容:
if ([[responseObject objectAtIndex:i] objectForKey:@"child"] == nil) {
}
Or a shorter alternative: 或更短的选择:
if (![[responseObject objectAtIndex:i] objectForKey:@"child"]) {
}
Another way to check is to use string length. 另一种检查方法是使用字符串长度。 I know other answers are just as good, I am just giving OP and anyone else in future some more options. 我知道其他答案也一样,将来我会给OP和其他任何人更多选择。
NSString *childStr = [[responseObject objectAtIndex:i]objectForKey:@"child"];
if ([childStr length] < 1)
{
//no value specified - childStr is NULL
}
else
{
//there is something in the childStr - throw a party!
}
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"hello"]) {
NSLog(@"only if not null show %@",[defaults objectForKey:@"hello"]);
}
This is a way to check it from User Defaults. 这是一种从“用户默认值”检查它的方法。 It will only print if the saved object does not equal null
. 仅当保存的对象不等于null
时才打印。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.