如何检测 NSString 是否为 null?
我有一段代码可以检测 NSString
是否为 NULL
、nil
等。但是,它崩溃了。这是我的代码:
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
” '
我是吗做错了什么?我需要用不同的方式来做吗?
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...";
}
}
And here's the error:
Terminating app due to uncaught exception '
NSInvalidArgumentException
', reason: '-[NSNull isEqualToString:]
: unrecognized selector sent to instance0x3eba63d4
'
Am I doing something wrong? Do I need to do it a different way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Objective-C 对象(类型
id
)的NULL
值为nil
。而
NULL
用于C指针(类型void *
)。(最终两者最终保持相同的值(
0x0
)。但是它们的类型不同。)在Objective-C中:
nil
< em>(全部小写) 为 null指向Objective-C 对象的指针。
Nil
(大写) 是空指针到Objective-C 类。
NULL
(全部大写) 是指向的空指针任何其他(C 指针,即)。
[NSNull null]
是一个单例,适用于无法使用 nil 的情况(向 NSArray 添加/接收 nil) /code>s 例如)在 Objective-C++ 中:
null
(小写)或nullptr
(C++11 或更高版本)是指向 C++ 对象 的空指针。因此,要检查
nil
,您应该显式与nil
(或NULL
)进行比较:或者让ObjC / C 为您隐式执行此操作:
这与 C 中的每个表达式一样(并且使用 Objective- C 是其超集)具有隐式布尔值:
现在,当检查
NSNull
时,这显然不起作用,因为[NSNull null]
返回一个指向NSNull
的单例实例,而不是nil
,因此它不等于0x0
。因此,要检查
NSNull
,可以使用:或(首选,请参阅注释):
请记住,如果
,后者(利用消息调用)将返回
恰好是false
>getCaptionnil
,虽然形式上是正确的,但可能不是您所期望/想要的。因此,如果一个人(无论出于何种原因)需要检查
nil
/NULL
和NSNull< /code>,必须将这两项检查结合起来:
有关形成等效肯定检查的帮助,请参阅德摩根定律和布尔否定。
编辑: NSHipster.com 刚刚发表了一篇关于 nil 之间细微差别的精彩文章,空等
The
NULL
value for Objective-C objects (typeid
) isnil
.While
NULL
is used for C pointers (typevoid *
).(In the end both end up holding the same value (
0x0
). They differ in type however.)In Objective-C:
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.)In Objective-C++:
null
(lowercase) ornullptr
(C++11 or later) is a null pointer to C++ objects.So to check against
nil
you should either compare againstnil
(orNULL
respectively) explicitly:or let ObjC / C do it implicitly for you:
This works as every expression in C (and with Objective-C being a superset thereof) has an implicit boolean value:
Now when checking for
NSNull
this obviously wouldn't work as[NSNull null]
returns a pointer to a singleton instance ofNSNull
, and notnil
, and therefore it is not equal to0x0
.So to check against
NSNull
one can either use:or (preferred, see comments):
Keep in mind that the latter (utilising a message call) will return
false
ifgetCaption
happens to benil
, which, while formally correct, might not be what you expect/want.Hence if one (for whatever reason) needed to check against both
nil
/NULL
andNSNull
, one would have to combine those two checks:For help on forming equivalent positive checks see De Morgan's laws and boolean negation.
Edit: NSHipster.com just published a great article on the subtle differences between nil, null, etc.
您应该使用
if ([myNSString isEqual:[NSNull null]])
这将检查对象 myNSString 是否等于 NSNull 对象。
You should use
if ([myNSString isEqual:[NSNull null]])
This will check if object myNSString is equal to NSNull object.
检查
NSNULL
的首选方法是Preferred Way to check for the
NSNULL
is您也可以这样做。
如果您想在未来防止 NSNull 的新子类出现,
You can also do
if you want to be future proof against new subclasses of NSNull.
只需检查此代码:
这应该可行。
Just check with this code:
This should work.