如何检查 NSNumber 中的空值

发布于 2024-10-03 03:09:03 字数 1277 浏览 4 评论 0原文

首先我承认我的无知,在我从事项目的几个月里我已经学到了关于 Objective-C 的一切。我还发现,Objective-C 似乎使我使用过的任何其他语言中的简单问题变得复杂,这非常令人沮丧。这个问题就是一个恰当的例子......

在第一次运行时,我的应用程序下载了一堆 JSON,它用于填充 CoreData 存储。我使用 Obj-C/JSON 库 (CJSONDeserializer) 将 JSON 转换为 NSArray。在一个 CoreData 实体的 JSON 下载中,有一个包含数字 ("show_id":2) 的字段,如果存在 1 或 null ("show_id":null),则标识另一个实体中的相关字段) 否则。在处理该字段时,我使用...将其分配给 NSNumber

NSNumber *shoIndex = [[item objectForKey:typeKey] objectForKey:@"show_id"];

然后,在尝试获取和获取之前,我尝试检查我是否有一个有效的数字。链接相关记录,以免在没有相关记录的情况下进行浪费处理。

询问 shoIndex with...

NSLog(@"shoIndex: %i, %@, %@", shoIndex, shoIndex, [shoIndex description]);

给出...

shoIndex: 19590600, <null>, <null>

,其中 JSON 值为 null &...

shoIndex: 228300880, 51, 51

否则。

到目前为止,我所做的唯一成功的检查是......

if (![[shoIndex description] isEqualToString:@"<null>"]) {

任何人都可以提出更好的方法吗?

更新...

以另一种方式将 shoIndex 分配为 NSNumber,有时包含 NSString@";”。 Obj-C 是否有类似 isa 运算符的东西,我可以用它来检查 shoIndex 内容的类型?

蒂亚,佩德罗。

First off I confess my ignorance, I've learned everything I know about Objective-C in the few months I've been working on my project. I also find it utterly frustrating how Objective-C seems to complicate what would be simple matters in any other language I've worked with. This question is a case in point...

In the first run my app downloads a bunch of JSON which it uses to populate a CoreData store. I use an Obj-C/JSON library (CJSONDeserializer) to convert the JSON to NSArrays. In the JSON download for one CoreData entity there's a field containing a number ("show_id":2) identifying the related field in another entity if there is one or null ("show_id":null) otherwise. In processing that field I assign it to an NSNumber using...

NSNumber *shoIndex = [[item objectForKey:typeKey] objectForKey:@"show_id"];

I then try to check that I have a valid number before attempting to fetch & link the related record so as to not do wasteful processing where there is no related record.

Interrogating shoIndex with...

NSLog(@"shoIndex: %i, %@, %@", shoIndex, shoIndex, [shoIndex description]);

Gives...

shoIndex: 19590600, <null>, <null>

where the JSON value was null &...

shoIndex: 228300880, 51, 51

otherwise.

So far the only successful check I've made is with...

if (![[shoIndex description] isEqualToString:@"<null>"]) {

Can anyone suggest a better way?

Update...

Looking at it another way shoIndex is assigned as a NSNumber that sometimes contains a NSString value @"<null>". Does Obj-C have something like an isa operator that I could use to check the type of the contents of shoIndex?

TIA, Pedro.

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

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

发布评论

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

评论(3

羁绊已千年 2024-10-10 03:09:03

使用[shoObject class]获取对象的类;因此,要测试 shoObject 的类,您可以使用

[shoObject isKindOfClass:[NSString class]];

一旦您整理出定义空字符串或 NSNumber 的标记,您就可以创建一个宏。我通过在名为 CommonMacros.h 的文件中保留 IsEmpty 宏来实现此目的。代码如下:

//Thanks Wil
//http://wilshipley.com/blog/2005/10/pimp-my-code-interlude-free-code.html

static inline BOOL IsEmpty(id thing) {
    return thing == nil
    || ([thing isEqual:[NSNull null]]) //JS addition for coredata
    || ([thing respondsToSelector:@selector(length)]
        && [(NSData *)thing length] == 0)
    || ([thing respondsToSelector:@selector(count)]
        && [(NSArray *)thing count] == 0);
}

然后,导入 CommonMacros.h 后,您可以像这样调用该函数:

if (IsEmpty(shotIndex)) {
    //do stuff
}

这应该可以解决这个问题,并且也适用于字符串、数组等,正如您从代码中看到的那样。感谢威尔·希普利!

Use [shoObject class] to get the class of an object; so, to test shoObject's class, you would use

[shoObject isKindOfClass:[NSString class]];

Once you've sorted out what markers define an empty string or NSNumber, you can create a macro. I do with this by keeping an IsEmpty macro in a file called CommonMacros.h. Here's the code:

//Thanks Wil
//http://wilshipley.com/blog/2005/10/pimp-my-code-interlude-free-code.html

static inline BOOL IsEmpty(id thing) {
    return thing == nil
    || ([thing isEqual:[NSNull null]]) //JS addition for coredata
    || ([thing respondsToSelector:@selector(length)]
        && [(NSData *)thing length] == 0)
    || ([thing respondsToSelector:@selector(count)]
        && [(NSArray *)thing count] == 0);
}

Then, after importing CommonMacros.h, you can call the function like this:

if (IsEmpty(shotIndex)) {
    //do stuff
}

This should take care of this problem, and will also work on strings, arrays, etc, as you can see from the code. Thanks to Wil Shipley!

_畞蕅 2024-10-10 03:09:03

在某些情况下,例如 NSUserDefaults 中缺少键,您将返回文字 @"" 作为空字符串。

这是我对 NSNumber 的安全检查。

请注意,首先检查它是否为 NSNumber,因为 NSNumber 不理解 isEqualToString

        id savedPin = [[NSUserDefaults standardUserDefaults] valueForKey:@"blah"];  // don't assume is created so don't type as NSNumber*
        if ( ![savedPin isKindOfClass:[NSNumber class]] && [savedPin isEqualToString:@""]) {  

In some cases, such as missing keys in NSUserDefaults, you get back the literal @"" as an empty string.

Here's my safe check for an NSNumber.

Note the check for it being an NSNumber occurs first because NSNumber doesn't understand isEqualToString

        id savedPin = [[NSUserDefaults standardUserDefaults] valueForKey:@"blah"];  // don't assume is created so don't type as NSNumber*
        if ( ![savedPin isKindOfClass:[NSNumber class]] && [savedPin isEqualToString:@""]) {  
柳絮泡泡 2024-10-10 03:09:03

在 Objective-c 中,它是 nil 而不是 null。所以:

if (shotIndex != nil) {
   // its not nil
}

In Objective-c, it's nil not null. So:

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