How to check the NULL value in NSString in iOS?

2019-02-13 16:36发布

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 execute the else condition.

Below is the code which I am using:

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];
}    

It always executes the else condition, and never the if condition, even when the value is NULL.

10条回答
做个烂人
2楼-- · 2019-02-13 16:39
  NSString *str;

  if ([[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]          isEqualToString:@""] || str==nil) 
  {

  }
查看更多
家丑人穷心不美
3楼-- · 2019-02-13 16:40

Use the following code:

-(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;

}

In Example -1, myString contains nothing. So the output is:

         myString is Null or Nil

In Example -2, sampleString contains some value. So the output is:

    sampleString contains iOS Programming
查看更多
霸刀☆藐视天下
4楼-- · 2019-02-13 16:42

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];
    }    
查看更多
霸刀☆藐视天下
5楼-- · 2019-02-13 16:49

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) 
查看更多
混吃等死
6楼-- · 2019-02-13 16:50

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.

查看更多
Bombasti
7楼-- · 2019-02-13 16:52

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];


    }
查看更多
登录 后发表回答