在Core Data中存储可选的NSNumber [英] Storing optional NSNumber in Core Data

查看:92
本文介绍了在Core Data中存储可选的NSNumber的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的核心数据模型中,我有一个具有可选NSNumber属性的实体。



如何测试以查看该属性中的值是否有效?



当我测试nil ...不工作。

  [self numberAttribute] == nil //总是返回NO 

当我测试一个零值int,这不工作。

  [[self numberAttribute] intValue] == 0 //始终不返回



事实上,[[self numberAttribute] intValue]]返回的东西看起来像一个指向内存地址的指针。



任何人都知道我在做什么?



EDIT:一个完全无关的代码部分。 NSNumber和Core Data的运行与预期的完全一样。也就是说,我将使用建议的方法使属性非可选并指定0作为默认值。



只需将此注释放在这里

解决方案

根据文档


您可以指定属性是可选属性,也就是说,它不需要有值。但是,一般来说,您不建议这样做 - 特别是对于数字值(通常可以使用具有默认值的强制属性,在模型为0时获得更好的结果)。这样做的原因是SQL对NULL有特殊的比较行为,这与Objective-C的nil不同。数据库中的NULL不等于0,并且搜索0将不匹配具有NULL的列。




  false ==(NULL == 0)
false ==(NULL!= 0)




此外,数据库中的NULL不等同于空字符串或空数据blob:




  false ==(NULL == @)
false ==(NULL!= @)

因此,尝试使用NULL,这不是nil。更好的是,设置默认值并将属性更改为非可选属性。这是我最后在我的模型中做的,在实践中它工作得很好。



编辑:
见我上面的评论。我尝试创建一个可选属性未设置的新受管对象,并且它为nil。

  BOOL isNil =([newManagedObject numberAttribute ] == nil); // equals YES 

另一个编辑:
我后来回来了,对象从调试器中的缓存加载(gdb是你的朋友!)。它试图访问内存位置0x0,这是NULL。


In my core data model, I have an entity with an optional NSNumber attribute.

How do I test to see if the value in that attribute is valid or not?

When I test for nil... doesn't work.

[self numberAttribute] == nil // always returns NO

When I test for a zero value int, that doesn't work.

[[self numberAttribute] intValue] == 0  // always returns no

As a matter of fact, [[self numberAttribute] intValue]] returns something that looks suspiciously like a pointer to a memory address.

Anyone have any idea what I am doing wrong?

EDIT: OK, the bug was in a totally unrelated part of the code. NSNumber and Core Data are functioning exactly as one would expect. That said, I am going to go with the suggested approach of making the attribute non-optional and specifying 0 as the default value.

Just putting this note here for a little while for anyone who was watching this question, then I am deleting it.

解决方案

According to the documentation,

You can specify that an attribute is optional—that is, it is not required to have a value. In general, however, you are discouraged from doing so—especially for numeric values (typically you can get better results using a mandatory attribute with a default value—in the model—of 0). The reason for this is that SQL has special comparison behavior for NULL that is unlike Objective-C's nil. NULL in a database is not the same as 0, and searches for 0 will not match columns with NULL.

false == (NULL == 0)
false == (NULL != 0)

Moreover, NULL in a database is not equivalent to an empty string or empty data blob, either:

false == (NULL == @"")
false == (NULL != @"")

So, try a test with NULL, which is not nil. Better yet, set a default and change the attribute to be non-optional. That's what I've ended up doing in my models, and in practice it works quite well.

Edit: See my comment above. I tried creating a new managed object with an optional attribute not set, and it was nil.

BOOL isNil = ([newManagedObject numberAttribute] == nil);  //equals YES

Another edit: I came back to this later and checked out an object loaded from the cache in the debugger (gdb is your friend!). It was trying to access memory location 0x0, which is NULL.

这篇关于在Core Data中存储可选的NSNumber的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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