简体   繁体   English

检查条件NOT <null> Objective C中NSString的值

[英]Condition for checking NOT <null> value for NSString in Objective C

Code: 码:

NSString *tempPhone = [NSString stringWithFormat:@"%@", [personDict objectForKey:kPhoneKey]];
NSLog(@"NSString *tempPhone = %@",tempPhone);

Output: 输出:

NSString *tempPhone = <null>

Now I want to add if condition, for not null ; 现在我想添加if条件,因为不为null ; I tried followings: 我试过以下:

if (tempEmail != NULL)
if (tempPhone != Nil)
if (tempPhone != nil)
if (![tempPhone compare:@"<null>"])
if (tempPhone != (NSString*)[NSNull null])

I also Checked this Post. 我也查了这篇帖子。

None of them is working for me. 他们都没有为我工作。 Where I am going wrong?? 我哪里错了?

Try the following code 请尝试以下代码

id phoneNo = [personDict objectForKey:kPhoneKey];

if( phoneNo && ![phoneNo isKindOfClass:[NSNull class]] )
{
    NSString *tempPhone = [NSString stringWithFormat:@"%@", [personDict objectForKey:kPhoneKey]];
    NSLog(@"NSString *tempPhone = %@",tempPhone);
}
else
{
    NSLog(@"NSString *tempPhone is null");
}

if ([str_name isKindOfClass:[NSNull class]]) if([str_name isKindOfClass:[NSNull class]])

works for me... 对我有用......

I think your personDict does not have an object for the key kPhoneKey , so it is returning nil . 我认为你的personDict没有关键kPhoneKey的对象,所以它返回nil When you format nil using %@ , you get the string " (null) ". 使用%@格式化nil ,会得到字符串“ (null) ”。

id object = [personDict objectForKey:kPhoneKey];
if (object) {
    NSString *tempPhone = [NSString stringWithFormat:@"%@", object];
    NSLog(@"NSString *tempPhone = %@",tempPhone);
} else {
    // object is nil
}
if (tempPhone != nil || [tempPhone isEqualToString:@"(null)"])

适合我。

就像这样 :

if (![tempPhone isKindOfClass:[NSNull class]] && tempPhone != nil)

First we need to check string length. 首先我们需要检查字符串长度。

if (tempPhone.length == 0) ` if(tempPhone.length == 0)`

{ {

//Your String is having empty value (Ex, (null)) //你的String有空值(Ex,(null))

} }

else 其他

{ {

// You having some values in this string //你在这个字符串中有一些值

}` }`

I Checked the webResponse (JSON, in my case). 我检查了webResponse(JSON,在我的例子中)。 It was returning me the string with value: 它给我返回了有价值的字符串:

<null>       

So, The following condition worked for me: 所以,以下条件对我有用:

if (![tempPhone isEqualToString:@"<null>"])

Thanks for all your time. 谢谢你所有的时间。 Thanks. 谢谢。

Because compare method returns type NSComparisonResult which is defined as 因为compare方法返回类型NSComparisonResult的定义为

enum _NSComparisonResult {NSOrderedAscending = -1, NSOrderedSame, NSOrderedDescending};
typedef NSInteger NSComparisonResult;

If the string is the same, it will return NSOrderedSame which have a NSInteger value of 0 . 如果字符串相同,它将返回NSInteger值为0 NSOrderedSame

Thus, the following line actually means... 因此,以下行实际上意味着......

if (![tempPhone compare:@"<null>"]) // `tempPhone` is equals to `@"<null>"`

or in a more understandable explanation, if tempPhone value is equal to @"<null>" . 或者在一个更容易理解的解释中,如果tempPhone值等于@"<null>"

You should write it as 你应该把它写成

if ([tempPhone compare:@"<null>"] != NSOrderedSame)

or 要么

if (![tempPhone isEqualString:@"<null>"])

In case the string is having a null value. 如果字符串具有空值。 Example in NSLog.. Implement this way.. NSLog中的示例..实现这种方式..

  if ([StringName isKindOfClass:[NSNULL Class]]) {

   }
   else {

   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM