7

我有一个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.

4

10 回答 10

36

在 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(所有属性的默认值),或者它被显式设置为单例,这将执行 nil 分支NSNull

于 2013-10-24T05:14:27.673 回答
3

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]])
于 2013-10-24T05:14:45.887 回答
0

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


    }
于 2013-10-24T05:20:57.073 回答
0
#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");

于 2015-01-27T12:27:01.903 回答
0

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

if (![string isKindOfClass:[NSNull class]] && string && string != NULL) 
于 2015-12-29T07:59:43.183 回答
0
+(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;
}

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

于 2017-01-07T13:57:04.307 回答
0

使用以下代码:

-(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
于 2016-08-04T04:48:35.643 回答
0

您可以使用 -[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];
    }    
于 2018-12-22T05:53:35.617 回答
-2
  NSString *str;

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

  }
于 2013-10-24T05:19:21.987 回答
-2

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

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

    }
于 2013-10-24T05:17:52.633 回答