如何检查 NSNumber 中的空值 [英] How to check for null value in NSNumber

查看:32
本文介绍了如何检查 NSNumber 中的空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

用...询问 shoIndex

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

给...

shoIndex: 19590600, <null>, <null>

JSON 值为 null &...

where the JSON value was null &...

shoIndex: 228300880, 51, 51

否则.

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

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

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

谁能提出更好的方法?

更新...

以另一种方式看待它 shoIndex 被分配为一个 NSNumber 有时包含一个 NSString@"<null>".Obj-C 是否有类似 isa 运算符之类的东西,我可以使用它来检查 shoIndex 内容的类型?

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,佩德罗.

推荐答案

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

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

[shoObject isKindOfClass:[NSString class]];

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

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);
}

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

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!

这篇关于如何检查 NSNumber 中的空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆