ios 如何检查 NSString 是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19546920/
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 NSString is null or not
提问by user2889249
I want to check weather a NSString
is null or not. Im assigning from an JSON array. After assigning that string value is <null>
. Now I want to check this string is null or not. So I put like this
我想检查天气 aNSString
是否为空。我从 JSON 数组分配。分配该字符串值后为<null>
. 现在我想检查这个字符串是否为空。所以我把这样
if (myStringAuthID==nil)
but this if statement always false. How I can check a string for null. Please help me
if (myStringAuthID==nil)
但是这个 if 语句总是错误的。如何检查字符串是否为空。请帮我
Thanks
谢谢
回答by Mirko Catalano
Like that:
像那样:
[myString isEqual: [NSNull null]];
回答by Hot Licks
There are three possible interpretations of "null" NSString:
“null” NSString 有三种可能的解释:
someStringPtr == nil
(id)someStringPtr == [NSNull null]
someStringPtr.length == 0
someStringPtr == nil
(id)someStringPtr == [NSNull null]
someStringPtr.length == 0
If you may have the possibility of all 3, the 3rd check subsumes the first, but I don't know of a simple check for all three.
如果您可能有所有 3 个检查的可能性,则第三个检查包含第一个,但我不知道对所有三个检查进行简单检查。
In general, JSON will return [NSNull null]
for a null
JSON value, but some kits may return @""
(length == 0) instead. nil
will never be used in iOS since it can't be placed in arrays/dictionaries.
通常,JSON 会返回[NSNull null]
一个null
JSON 值,但有些工具包可能会返回@""
(length == 0)。 nil
永远不会在 iOS 中使用,因为它不能放在数组/字典中。
回答by Nicholas Smith
Try if(myString == [NSNull null])
. That should evaluate it properly.
试试if(myString == [NSNull null])
。那应该正确评估它。
回答by Andrea
I think that is best if you check before cast it to an NSString or whatever, you have different options, the above are correct, but I prefer this:
我认为最好在将其转换为 NSString 之前进行检查,您有不同的选择,以上是正确的,但我更喜欢这个:
id NilOrValue(id aValue) {
if ((NSNull *)aValue == [NSNull null]) {
return nil;
}
else {
return aValue;
}
}
Using this snippet (pay attention that is a C function) before passing the value to a pointer you can safely pass a value or nil if the value in NSNull. Passing nil is great, because if you send a message to a nil object, it doesn't throw an exception. You can also check for class type with -isKindOfClass
.
在将值传递给指针之前使用此代码段(注意它是一个 C 函数),您可以安全地传递一个值或 nil(如果该值在 NSNull 中)。传递 nil 很棒,因为如果您向 nil 对象发送消息,它不会抛出异常。您还可以使用-isKindOfClass
.
回答by JonahGabriel
Here is part of a string category I created:
这是我创建的字符串类别的一部分:
@interface NSString (Enhancements)
+(BOOL)isNullOrEmpty:(NSString *)inString;
@end
@implementation NSString (Enhancements)
+(BOOL)isNullOrEmpty:(NSString *)inString
{
BOOL retVal = YES;
if( inString != nil )
{
if( [inString isKindOfClass:[NSString class]] )
{
retVal = inString.length == 0;
}
else
{
NSLog(@"isNullOrEmpty, value not a string");
}
}
return retVal;
}
@end