【问题标题】:How to check the NULL value in NSString in iOS?如何在 iOS 中检查 NSString 中的 NULL 值?
【发布时间】:2018-12-22 14:13:54
【问题描述】:

我有一个NSString,我想检查它是否有一个NULL 值。如果是这样,那么应该执行if 条件。否则它应该执行else 条件。

下面是我正在使用的代码:

if ([appDelegate.categoryName isEqual:[NSNull null]])
{
    select = [[NSString alloc] initWithFormat:@"select * FROM ContentMaster LEFT JOIN Category ON ContentMaster.CategoryID=Category.CategoryID where ContentMaster.ContentTagText='%@'", appDelegate.tagInput];
}
else
{
    select = [[NSString alloc] initWithFormat:@"select * FROM ContentMaster LEFT JOIN Category ON ContentMaster.CategoryID=Category.CategoryID LEFT JOIN Topic ON ContentMaster.TopicID=Topic.TopicID where ContentMaster.ContentTagText='%@' && Category.CategoryName='%@' && Topic.TopicName='%@'", appDelegate.tagInput, appDelegate.categoryName, appDelegate.topicName];
}    

它总是执行else 条件,而不是if 条件,即使值是NULL

【问题讨论】:

标签: ios null nsstring


【解决方案1】:

在 Objective-C 和 Cocoa 中,该属性可能没有被设置——即nil——或者它可能被设置为nil 的对象表示,它是NSNull 的一个实例。您可能想要检查这些条件中的任何一个,如下所示:

NSString* categoryName = appDelegate.categoryName;
if (categoryName == nil || categoryName == (id)[NSNull null]) {
  // nil branch
} else {
  // category name is set
}

如果categoryName 属性设置为nil(所有属性的默认值),或者已明确设置为NSNull 单例,这将执行nil 分支。

【讨论】:

  • 谢谢它的工作,但你能告诉我一件事,否则我已经给了查询它很好,但它没有返回任何东西
  • SQL is select * FROM ContentMaster LEFT JOIN Category ON ContentMaster.CategoryID= Category.CategoryID LEFT JOIN Topic ON ContentMaster.TopicID=Topic.TopicID where ContentMaster.ContentTagText='Good' && Category.CategoryName=' Product' && Topic.TopicName='M2M 运营'
  • 这是在else中执行的查询日志
  • @QueenSolutions 你是什么意思它不返回任何东西? SQL查询没有结果,还是说select为nil?
  • @QueenSolutions 我建议提出一个新问题并将更多代码放入新问题的循环中。询问为什么没有在那里设置值,因为这个问题是关于nil 检查。
【解决方案2】:

Objective-C 对象(类型 id)的 NULL 值为 nil。

虽然 NULL 用于 C 指针(类型 void *)。

(最终两者都持有相同的值(0x0)。但是它们的类型不同。)

在 Objective-C 中:

nil (all lower-case) is a null pointer to an Objective-C object.
Nil (capitalized) is a null pointer to an Objective-C class.
NULL (all caps) is a null pointer to anything else (C pointers, that is).
[NSNull null] (singleton) for situations where use of nil is not possible (adding/receiving nil to/from NSArrays e.g.)

所以要检查 NSNull,可以使用:

if ((NSNull *)myString == [NSNull null])

或者如果想省略转换为 NSNull 的需要:

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

【讨论】:

    【解决方案3】:

    尝试使用它。检查您的值是否属于 NULL 类,而不是比较指针值。

    if ([appDelegate.categoryName isKindOfClass:[NSNull class]]){
    
            select = [[NSString alloc] initWithFormat:@"select * FROM ContentMaster LEFT JOIN  Category  ON  ContentMaster.CategoryID= Category.CategoryID where ContentMaster.ContentTagText='%@'",appDelegate.tagInput];
    
            }
    
        else {
    
    
          select = [[NSString alloc] initWithFormat:@"select * FROM ContentMaster LEFT JOIN  Category  ON  ContentMaster.CategoryID= Category.CategoryID  LEFT JOIN Topic ON ContentMaster.TopicID=Topic.TopicID where ContentMaster.ContentTagText='%@' && Category.CategoryName='%@' && Topic.TopicName='%@'",appDelegate.tagInput,appDelegate.categoryName,appDelegate.topicName];
    
    
        }
    

    【讨论】:

    • NSNull 单例进行指针比较会更高效、更容易阅读。您还需要检查该属性是否根本没有设置(几乎可以肯定是这里的问题)。避免进行类比较,尤其是在大多数情况下它们会失败时——也就是说,这个值不会是 NSNull 单例。
    【解决方案4】:
    #define SAFESTRING(str) ISVALIDSTRING(str) ? str : @""
    #define ISVALIDSTRING(str) (str != nil && [str isKindOfClass:[NSNull class]] == NO)
    #define VALIDSTRING_PREDICATE [NSPredicate predicateWithBlock:^(id evaluatedObject, NSDictionary *bindings) {return (BOOL)ISVALIDSTRING(evaluatedObject);}]
    

    SAFESTRING("PASS_OBJECT_HERE");

    【讨论】:

      【解决方案5】:

      在检查空值时最好采取更安全的方式,因为它可能导致崩溃。

      if (![string isKindOfClass:[NSNull class]] && string && string != NULL) 
      

      【讨论】:

        【解决方案6】:

        使用以下代码:

        -(void)viewDidLoad {
          [super viewDidLoad];
          //Example - 1
            NSString *myString;
            if([[self checkForNull:myString] isEqualToString:@""]){
        
                NSLog(@"myString is Null or Nil");
        
           }
           else{
        
             NSLog(@"myString contains %@",myString);
        
           }
        
         //Example - 2
           NSString *sampleString = @"iOS Programming";
            if([[self checkForNull:sampleString] isEqualToString:@""]){
        
                NSLog(@"sampleString is Null or Nil");
        
           }
           else{
        
             NSLog(@"sampleString contains %@",sampleString);
        
           }
        
        
        }
        
        -(id)checkForNull:(id)value{
            if ([value isEqual:[NSNull null]]) {
        
                    return @"";
           }
        
            else if (value == nil)
        
                    return @"";
            return value;
        
        }
        

        在示例 -1 中,myString 不包含任何内容。所以输出是:

                 myString is Null or Nil
        

        在示例 -2 中,sampleString 包含一些值。所以输出是:

            sampleString contains iOS Programming
        

        【讨论】:

          【解决方案7】:
          +(BOOL)isEmpty:(NSString *)str{
              if (str == nil || str == (id)[NSNull null] || [[NSString stringWithFormat:@"%@",str] length] == 0 || [[[NSString stringWithFormat:@"%@",str] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0){
                  return YES;
              }
              return NO;
          }
          

          只需在 Method 中传递您的字符串 :)

          【讨论】:

            【解决方案8】:

            你可以通过使用 - [NSNull class] 来做到这一点

            if ([appDelegate.categoryName isEqual:[NSNull class]])
                {
                    select = [[NSString alloc] initWithFormat:@"select * FROM ContentMaster LEFT JOIN Category ON ContentMaster.CategoryID=Category.CategoryID where ContentMaster.ContentTagText='%@'", appDelegate.tagInput];
                }
                else
                {
                    select = [[NSString alloc] initWithFormat:@"select * FROM ContentMaster LEFT JOIN Category ON ContentMaster.CategoryID=Category.CategoryID LEFT JOIN Topic ON ContentMaster.TopicID=Topic.TopicID where ContentMaster.ContentTagText='%@' && Category.CategoryName='%@' && Topic.TopicName='%@'", appDelegate.tagInput, appDelegate.categoryName, appDelegate.topicName];
                }    
            

            【讨论】:

              【解决方案9】:

              还添加一个额外的长度检查。这肯定会奏效。

              if ([appDelegate.categoryName isEqual:[NSNull null]] && appDelegate.categoryName.length>0){
              
              
                      select = [[NSString alloc] initWithFormat:@"select * FROM ContentMaster LEFT JOIN  Category  ON  ContentMaster.CategoryID= Category.CategoryID where ContentMaster.ContentTagText='%@'",appDelegate.tagInput];
              
                      }        else {
                        select = [[NSString alloc] initWithFormat:@"select * FROM ContentMaster LEFT JOIN  Category  ON  ContentMaster.CategoryID= Category.CategoryID  LEFT JOIN Topic ON ContentMaster.TopicID=Topic.TopicID where ContentMaster.ContentTagText='%@' && Category.CategoryName='%@' && Topic.TopicName='%@'",appDelegate.tagInput,appDelegate.categoryName,appDelegate.topicName];
              
                  }
              

              【讨论】:

              • [appDelegate.categoryName isEqual:[NSNull null]] && appDelegate.categoryName.length > 0 你可能想重新学习布尔运算。
              • 是的,所写的语句将导致应用程序崩溃,因为根据定义,如果categoryNameNSNull 单例,它不会响应length 消息。
              【解决方案10】:
                NSString *str;
              
                if ([[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]          isEqualToString:@""] || str==nil) 
                {
              
                }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2010-09-27
                • 2011-08-25
                • 2022-09-23
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多