如何检查 NSNumber 中的空值
首先我承认我的无知,在我从事项目的几个月里我已经学到了关于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
[shoObject class]
获取对象的类;因此,要测试shoObject
的类,您可以使用一旦您整理出定义空字符串或 NSNumber 的标记,您就可以创建一个宏。我通过在名为 CommonMacros.h 的文件中保留 IsEmpty 宏来实现此目的。代码如下:
然后,导入 CommonMacros.h 后,您可以像这样调用该函数:
这应该可以解决这个问题,并且也适用于字符串、数组等,正如您从代码中看到的那样。感谢威尔·希普利!
Use
[shoObject class]
to get the class of an object; so, to testshoObject
's class, you would useOnce 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:
Then, after importing CommonMacros.h, you can call the function like this:
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!
在某些情况下,例如 NSUserDefaults 中缺少键,您将返回文字 @"" 作为空字符串。
这是我对 NSNumber 的安全检查。
请注意,首先检查它是否为 NSNumber,因为 NSNumber 不理解 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
在 Objective-c 中,它是
nil
而不是null
。所以:In Objective-c, it's
nil
notnull
. So: