How to check the NULL value in NSString in iOS?

前端 未结 10 1526
盖世英雄少女心
盖世英雄少女心 2021-01-02 15:17

I have an NSString and I want to check if it has a NULL value. If it does, then the if condition should execute. Else it should execut

相关标签:
10条回答
  • 2021-01-02 15:44

    The NULL value for Objective-C objects (type id) is nil.

    While NULL is used for C pointers (type void *).

    (In the end both end up holding the same value (0x0). They differ in type however.)

    In 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.)
    

    So to check against NSNull one can either use:

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

    or if one wants to omit the need of casting to NSNull:

    if ([myString isKindOfClass:[NSNull class]])
    
    0 讨论(0)
  • 2021-01-02 15:46

    In Objective-C and Cocoa, the property may not be set—that is, it's nil—or it may be set to the object representation of nil, which is an instance of NSNull. You probably want to check for either of these conditions, like this:

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

    This will execute the nil branch if the categoryName property is set to nil (the default for all properties), or if it's been explicitly set to the NSNull singleton.

    0 讨论(0)
  • 2021-01-02 15:51

    You can do it by using - [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];
        }    
    
    0 讨论(0)
  • 2021-01-02 15:57

    Its better to be on safer side in checking null values , as it can lead to crash.

    if (![string isKindOfClass:[NSNull class]] && string && string != NULL) 
    
    0 讨论(0)
  • 2021-01-02 15:58

    Try to use this. Check your value is kind of NULL class or not rather than comparing Pointers value.

    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];
    
    
        }
    
    0 讨论(0)
  • 2021-01-02 16:03

    Add an additional check for length also. This will definitely work.

    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];
    
        }
    
    0 讨论(0)
提交回复
热议问题