简体   繁体   English

如何在NSNumber中检查空值

[英]How to check for null value in NSNumber

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. 首先,我承认自己的无知,在我参与项目的几个月里,我已经学会了所有关于Objective-C的知识。 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. 我还发现Objective-C似乎使我用过的任何其他语言中的简单问题变得复杂,这让我感到非常沮丧。 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. 在第一次运行中,我的应用程序下载了一堆JSON,用于填充CoreData存储。 I use an Obj-C/JSON library (CJSONDeserializer) to convert the JSON to NSArrays. 我使用Obj-C / JSON库(CJSONDeserializer)将JSON转换为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. 在一个CoreData实体的JSON下载中,有一个字段包含一个数字( "show_id":2 ),如果有另一个实体,则标识相关字段,否则为null( "show_id":null )。 In processing that field I assign it to an NSNumber using... 在处理该字段时,我使用...将其分配给NSNumber

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... 用...询问shoIndex

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

Gives... 给...

shoIndex: 19590600, <null>, <null>

where the JSON value was null &... 其中JSON值为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>" . 另一种方式是将shoIndex指定为NSNumber ,有时包含NSString@"<null>" Does Obj-C have something like an isa operator that I could use to check the type of the contents of shoIndex ? Obj-C有类似isa运算符的东西,我可以用它来检查shoIndex的内容类型吗?

TIA, Pedro. TIA,Pedro。

Use [shoObject class] to get the class of an object; 使用[shoObject class]获取对象的类; so, to test shoObject 's class, you would use 所以,要测试shoObject的课程,你会使用

[shoObject isKindOfClass:[NSString class]];

Once you've sorted out what markers define an empty string or NSNumber, you can create a macro. 一旦你整理出哪些标记定义了一个空字符串或NSNumber,你就可以创建一个宏。 I do with this by keeping an IsEmpty macro in a file called CommonMacros.h. 我通过将IsEmpty宏保存在名为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: 然后,在导入CommonMacros.h之后,您可以像这样调用函数:

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! 感谢Wil Shipley!

In some cases, such as missing keys in NSUserDefaults, you get back the literal @"" as an empty string. 在某些情况下,例如NSUserDefaults中缺少键,您将文字@“”作为空字符串返回。

Here's my safe check for an NSNumber. 这是我对NSNumber的安全检查。

Note the check for it being an NSNumber occurs first because NSNumber doesn't understand isEqualToString 请注意,首先检查它是否为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 Objective-c, it's nil not null . 在Objective-C,它是nil不是null So: 所以:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM