如何检查 NSNumber 中的空值

How to check for null value in NSNumber(如何检查 NSNumber 中的空值)
本文介绍了如何检查 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 中的空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type(异常:不应为错误类型创建SimpleTypeImpl)
Android IllegalArgumentException: The tag for fragment_XXX is invalid. Received: layout-sw600dp/fragment_XXX_0(Android IlLegalArgumentException:Fragment_XXX的标签无效。收到:Layout-sw600dp/Fragment_XXX_0)
iOS convert audio sample rate from 16 kHz to 8 kHz(IOS将音频采样率从16 kHz转换为8 kHz)
Enforcing an audio sampling rate in iOS(在iOS中强制音频采样率)
messaging().onNotificationOpenedApp is never triggered, messaging().getInitialNotification() is triggered but remoteMessage is always null(消息传递().onNotificationOpenedApp从不被触发,消息传递().getInitialNotification()被触发,但远程消息始终为空)