【问题标题】:Condition for checking NOT <null> value for NSString in Objective C在 Objective C 中检查 NSString 的 NOT <null> 值的条件
【发布时间】:2012-01-05 06:11:04
【问题描述】:

代码:

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

输出:

NSString *tempPhone = <null>

现在我想添加 if 条件,对于 not null;我尝试了以下方法:

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

我也查了this Post.

它们都不适合我。我哪里错了??

【问题讨论】:

  • 你实现if([tempPhone length]==0)条件了吗?我认为这会对你有所帮助。
  • 表示您正在获取具有空值的字符串长度?
  • 是的,它是“6”。我认为这是一个包含 "" 的字符串

标签: iphone objective-c ios if-statement


【解决方案1】:

试试下面的代码

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");
}

【讨论】:

【解决方案2】:

我认为您的 personDict 没有键 kPhoneKey 的对象,因此它返回 nil。当您使用%@ 格式化nil 时,您会得到字符串“(null)”。

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

【讨论】:

  • 对象不是零;它实际上是一个字符串,值为“”。谢谢你的时间。
【解决方案3】:
if (tempPhone != nil || [tempPhone isEqualToString:@"(null)"])

为我工作。

【讨论】:

    【解决方案4】:

    是这样的:

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

    【讨论】:

      【解决方案5】:

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

      为我工作...

      【讨论】:

        【解决方案6】:

        我检查了 webResponse(在我的例子中是 JSON)。 它向我返回了带有值的字符串:

        <null>       
        

        所以,以下条件对我有用:

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

        感谢您的所有时间。谢谢。

        【讨论】:

        • 通常不是字符串。它是一个 NSNull 对象。
        • 然后你像这样检查它:[tempPhone isKindOfClass:[NSNull class]]
        【解决方案7】:

        因为compare 方法返回类型NSComparisonResult 定义为

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

        如果字符串相同,则返回NSOrderedSame,其NSInteger值为0

        因此,下面这行实际上意味着...

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

        或者在更容易理解的解释中,如果tempPhone 的值等于@"&lt;null&gt;"

        你应该写成

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

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

        【讨论】:

          【解决方案8】:

          如果字符串具有空值。 NSLog 中的示例.. 以这种方式实现..

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

          【讨论】:

            【解决方案9】:

            首先我们需要检查字符串长度。

            如果 (tempPhone.length == 0) `

            {

            //你的字符串有空值 (E.x, (null))

            }

            其他

            {

            // 你在这个字符串中有一些值

            }`

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2015-08-08
              • 2014-12-28
              • 2011-09-25
              • 2015-02-11
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多