如何在iOS中检查NSString中的NULL值?

时间:2013-10-24 05:07:46

标签: ios null nsstring

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

10 个答案:

答案 0 :(得分:34)

在Objective-C和Cocoa中,可能没有设置属性 - 即nil - 或者它可能被设置为nil的对象表示,NSNull是{NSString* categoryName = appDelegate.categoryName; if (categoryName == nil || categoryName == (id)[NSNull null]) { // nil branch } else { // category name is set } 的实例1}}。您可能想要检查以下任一条件,如下所示:

categoryName

如果nil属性设置为NSNull(所有属性的默认值),或者已将其显式设置为{{1}}单例,则执行nil分支。

答案 1 :(得分:3)

Objective-C对象的NULL值(类型id)为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]])

答案 2 :(得分:0)

尝试使用此功能。检查您的值是否为NULL类,而不是比较Pointers值。

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


    }

答案 3 :(得分: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&#34);

答案 4 :(得分:0)

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

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

答案 5 :(得分: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

答案 6 :(得分: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;
}

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

答案 7 :(得分: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];
    }    

答案 8 :(得分:-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];

    }

答案 9 :(得分:-2)

  NSString *str;

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

  }