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

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

问题描述

首先我承认我的无知,我已经了解了我在我的项目工作的几个月内我知道的关于Objective-C的一切。我也发现它完全令人沮丧的是如何Objective-C似乎使任何其他语言,我曾经与简单的事情复杂。这个问题是一个例子。



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

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

然后我尝试检查我有一个有效的数字,链接相关记录,以便在没有相关记录的情况下不进行浪费处理。



shoIndex 一起询问。 ..

  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 value @< null>。 Obj-C有类似 isa 运算符,我可以用来检查 shoIndex 的内容类型?



TIA,Pedro。

解决方案

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

  shoObject isKindOfClass:[NSString class]]; 

一旦你整理出了什么标记定义一个空字符串或NSNumber,你可以创建一个宏。我通过在名为CommonMacros.h的文件中保留一个IsEmpty宏来做这件事。以下是代码:

  //感谢Wil 
//hilp://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添加为coredata
|| ([something responsesToSelector:@selector(length)]
&& [(NSData *)thing length] == 0)
|| ([something respondingToSelector:@selector(count)]
&& [(NSArray *)thing count] == 0);
}

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

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

这应该解决这个问题,并且也可以处理字符串,数组等, 。感谢Wil Shipley!


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.

解决方案

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!

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

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