如何检测 NSString 是否为 null?

发布于 2024-11-01 21:07:19 字数 844 浏览 4 评论 0原文

我有一段代码可以检测 NSString 是否为 NULLnil 等。但是,它崩溃了。这是我的代码:

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 instance 0x3eba63d4'

Am I doing something wrong? Do I need to do it a different way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

听不够的曲调 2024-11-08 21:07:19

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 (小写)nullptrC++11 或更高版本)是指向 C++ 对象 的空指针。

因此,要检查nil,您应该显式nil(或NULL)进行比较:

if (getCaption == nil) ...

或者让ObjC / C 为您隐式执行此操作:

if (!getCaption) ...

这与 C 中的每个表达式一样(并且使用 Objective- C 是其超集)具有隐式布尔值:

expression != 0x0 => true
expression == 0x0 => false

现在,当检查 NSNull 时,这显然不起作用,因为 [NSNull null] 返回一个指向NSNull 的单例实例,而不是 nil,因此它不等于 0x0

因此,要检查 NSNull ,可以使用:

if ((NSNull *)getCaption == [NSNull null]) ...

或(首选,请参阅注释):

if ([getCaption isKindOfClass:[NSNull class]]) ...

请记住,如果 ,后者(利用消息调用)将返回 false >getCaption 恰好是 nil,虽然形式上是正确的,但可能不是您所期望/想要的。

因此,如果一个人(无论出于何种原因)需要检查 nil/NULL NSNull< /code>,必须将这两项检查结合起来:

if (!getCaption || [getCaption isKindOfClass:[NSNull class]]) ...

有关形成等效肯定检查的帮助,请参阅德摩根定律布尔否定

编辑: NSHipster.com 刚刚发表了一篇关于 nil 之间细微差别的精彩文章,空等

The NULL value for Objective-C objects (type id) is nil.

While NULL is used for C pointers (type void *).

(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 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] is a singleton for situations where use of nil is not possible (adding/receiving nil to/from NSArrays e.g.)

In Objective-C++:

  • All of the above, plus:
  • null (lowercase) or nullptr (C++11 or later) is a null pointer to C++ objects.

So to check against nil you should either compare against nil (or NULL respectively) explicitly:

if (getCaption == nil) ...

or let ObjC / C do it implicitly for you:

if (!getCaption) ...

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

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.

So to check against NSNull one can either use:

if ((NSNull *)getCaption == [NSNull null]) ...

or (preferred, see comments):

if ([getCaption isKindOfClass:[NSNull class]]) ...

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.

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]]) ...

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.

善良天后 2024-11-08 21:07:19

您应该使用

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.

请远离我 2024-11-08 21:07:19

检查 NSNULL 的首选方法是

if(!getCaption || [getCaption isKindOfClass:[NSNull class]])   

Preferred Way to check for the NSNULL is

if(!getCaption || [getCaption isKindOfClass:[NSNull class]])   
盛夏已如深秋| 2024-11-08 21:07:19
if([getCaption class] == [NSNull class])
    ...

您也可以这样做。

if([getCaption isKindOfClass:[NSNull class]])
    ...

如果您想在未来防止 NSNull 的新子类出现,

if([getCaption class] == [NSNull class])
    ...

You can also do

if([getCaption isKindOfClass:[NSNull class]])
    ...

if you want to be future proof against new subclasses of NSNull.

纸短情长 2024-11-08 21:07:19

只需检查此代码:

NSString *object;

if(object == nil)

这应该可行。

Just check with this code:

NSString *object;

if(object == nil)

This should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文