如何在 iOS 中检查 NSString 中的 NULL 值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19557392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check the NULL value in NSString in iOS?
提问by Queen Solutions
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.
我有一个NSString
,我想检查它是否NULL
有价值。如果是,那么if
条件应该执行。否则它应该执行else
条件。
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
.
它始终执行else
条件,从不执行if
条件,即使值为NULL
。
回答by Jason Coco
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:
在 Objective-C 和 Cocoa 中,该属性可能不会被设置——也就是说,它是nil
——或者它可能被设置为 的对象表示nil
,它是 的一个实例NSNull
。您可能想要检查这些条件中的任何一个,如下所示:
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.
如果categoryName
属性设置为nil
(所有属性的默认值),或者它已显式设置为NSNull
单例,这将执行 nil 分支。
回答by Abhinav
The NULL value for Objective-C objects (type id) is nil.
Objective-C 对象(类型 id)的 NULL 值为 nil。
While NULL is used for C pointers (type void *).
而 NULL 用于 C 指针(类型 void *)。
(In the end both end up holding the same value (0x0). They differ in type however.)
(最终两者都保持相同的值(0x0)。但是它们的类型不同。)
In Objective-C:
在 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:
因此,要检查 NSNull 可以使用:
if ((NSNull *)myString == [NSNull null])
or if one wants to omit the need of casting to NSNull:
或者,如果您想省略强制转换为 NSNull 的需要:
if ([myString isKindOfClass:[NSNull class]])
回答by Satish Azad
Try to use this. Check your value is kind of NULL class or not rather than comparing Pointers value.
尝试使用这个。检查您的值是否为 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];
}
回答by Darshit Shah
#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");
SAFESTRING("PASS_OBJECT_HERE");
回答by coreDeviOS
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)
回答by KSR
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:
在示例 -1 中, myString 不包含任何内容。所以输出是:
myString is Null or Nil
In Example -2, sampleString contains some value. So the output is:
在示例 -2 中,sampleString 包含一些值。所以输出是:
sampleString contains iOS Programming
回答by Janak Thakkar
+(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;
}
Just pass your string in Method :)
只需在方法中传递您的字符串:)
回答by TheTravloper
You can do it by using - [NSNull class]
您可以使用 - [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];
}
回答by Itesh
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];
}
回答by Varsha Vijayvargiya
NSString *str;
if ([[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] isEqualToString:@""] || str==nil)
{
}