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

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

在Objective-C和Cocoa中,属性可能不会被设置,也就是说,它可以被设置为nil的对象表示,这是一个NSNull的实例。 您可能想检查这些条件之一,如下所示:

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

如果categoryName属性设置为nil (所有属性的默认值),或者它被显式设置为NSNull单例,那么这将执行nil分支。

Objective-C对象的NULL值(typesid)为零。

NULL用于C指针(types为void *)。

(最后两端保持相同的值(0x0),但它们的types不同)。

在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 eg) 

所以要检查NSNull可以使用:

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

或者如果想省略投射到NSNull的需要:

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

尝试使用这个。 检查你的值是否是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]; } 
 #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”);

最好在检查空值时更安全一些,因为这会导致崩溃。

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

使用下面的代码:

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

只要在方法中传递你的string:)

再增加一个检查长度也。 这一定会工作。

 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]; } 
  NSString *str; if ([[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] isEqualToString:@""] || str==nil) { }