问题描述
我有一段代码可以检测NSString
是否为NULL
、nil
等.但是,它崩溃了.这是我的代码:
I have a piece of code that detects if a NSString
is NULL
, nil
, etc. However, it crashes. Here is my code:
NSArray *resultstwo = [database executeQuery:@"SELECT * FROM processes WHERE ready='yes' LIMIT 0,1"];
for (NSDictionary *rowtwo in resultstwo) {
NSString *getCaption = [rowtwo valueForKey:@"caption"];
if (getCaption == NULL) {
theCaption = @"Photo uploaded...";
} else if (getCaption == nil) {
theCaption = @"Photo uploaded...";
} else if ([getCaption isEqualToString:@""]) {
theCaption = @"Photo uploaded...";
} else if ([getCaption isEqualToString:@" "]) {
theCaption = @"Photo uploaded...";
}
}
这是错误:
由于未捕获的异常NSInvalidArgumentException
"而终止应用,原因:-[NSNull isEqualToString:]
:无法识别的选择器发送到实例 0x3eba63d4
'
我做错了吗?我需要以不同的方式来做吗?
Am I doing something wrong? Do I need to do it a different way?
推荐答案
Objective-C 对象的 NULL
值(类型 id
)是 nil
.
而 NULL
用于 C 指针(类型 void *
).
While NULL
is used for C pointers (type void *
).
(最终两者都持有相同的值(0x0
).但是它们的类型不同.)
(In the end both end up holding the same value (0x0
). They differ in type however.)
在 Objective-C 中:
nil
(全部小写) 为空指向 Objective-C 对象的指针.Nil
(大写) 是一个空指针到 Objective-C 类.NULL
(all caps) 是一个指向其他任何(C 指针,即).[NSNull null]
是单例,用于不可能使用 nil 的情况(向/从NSArray
s eg)
nil
(all lower-case) is a nullpointer to an Objective-C object.Nil
(capitalized) is a null pointerto an Objective-C class.NULL
(all caps) is a null pointer toanything else (C pointers, that is).[NSNull null]
is a singleton for situations where use of nil is not possible (adding/receiving nil to/fromNSArray
s e.g.)
在Objective-C++中:
- 以上所有,加上:
null
(小写) 或nullptr
(C++11 或更高版本)是指向C++ 对象.
- All of the above, plus:
null
(lowercase) ornullptr
(C++11 or later) is a null pointer to C++ objects.
因此要检查 nil
,您应该与 nil
(或分别为 NULL
)进行比较明确:
So to check against nil
you should either compare against nil
(or NULL
respectively) explicitly:
if (getCaption == nil) ...
或者让 ObjC/C隐式为你做:
if (!getCaption) ...
这是因为 C 中的每个表达式(并且 Objective-C 是其超集)都有一个隐含的布尔值:
This works as every expression in C (and with Objective-C being a superset thereof) has an implicit boolean value:
expression != 0x0 => true
expression == 0x0 => false
现在,当检查 NSNull
时,这显然不起作用,因为 [NSNull null]
返回一个指向 NSNull
的单例实例的指针,而不是 nil
,因此它不等于 0x0
.
Now when checking for NSNull
this obviously wouldn't work as [NSNull null]
returns a pointer to a singleton instance of NSNull
, and not nil
, and therefore it is not equal to 0x0
.
所以要检查 NSNull
可以使用:
So to check against NSNull
one can either use:
if ((NSNull *)getCaption == [NSNull null]) ...
或(首选,见评论):
if ([getCaption isKindOfClass:[NSNull class]]) ...
请记住,如果 getCaption
恰好是 nil
,后者(使用消息调用)将返回 false
,虽然正式正确,可能不是您期望/想要的.
Keep in mind that the latter (utilising a message call) will return false
if getCaption
happens to be nil
, which, while formally correct, might not be what you expect/want.
因此,如果一个人(无论出于何种原因)需要同时检查 nil
/NULL
和 NSNull
,必须结合这两项检查:
Hence if one (for whatever reason) needed to check against both nil
/NULL
and NSNull
, one would have to combine those two checks:
if (!getCaption || [getCaption isKindOfClass:[NSNull class]]) ...
有关形成等效肯定检查的帮助,请参阅德摩根定律a> 和 布尔否定.
For help on forming equivalent positive checks see De Morgan's laws and boolean negation.
NSHipster.com 刚刚发表了一篇关于细微差别的精彩文章介于 nil、null 等之间
NSHipster.com just published a great article on the subtle differences between nil, null, etc.
这篇关于如何检测 NSString 是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!