if语句失败,使用CoreData受管对象的变量 [英] if statement fails with variable from CoreData managed object

查看:168
本文介绍了if语句失败,使用CoreData受管对象的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是一个真正的doozy,也许有人可以帮助。



所以我有一个称为格式的被管对象类,它是一个实体。无论如何,它有一个名为slot的属性是NSNumber的属性。现在数字可以有从零到四的值,但如果它没有值,那么我想NSNumber对象等于nil,而不是零。我没有运气,因为显然是零是相同的没有。啊。 (有没有办法判断指针是否为空,nil,而不是指向一个零值?)



无论如何,我试过如下:

  if(@myFormat.slot!= @ (null))
NSLog(@slot ==%@,myFormat.slot);

但问题是我在我的日志中有这个:

  slot ==(null)

所以,OK ...什么鬼?如果@myFormat.slot== @(null)那么怎么是如果语句解析...?!?



解决方案

你不会得到一个 nil 从属性返回。相反,你会得到一个 [NSNull null] 对象。



在Objective-C中, nil null 不可互换。当你看到 nil 时,你几乎看到一个取消引用的指针。 nil 旨在表示尚未将任何对象分配给符号。相比之下, null [NSNull null] 的单例实例。它用作占位符,表示尚未设置由对象表示的某些 。换句话说, nil 的值在Objective-C中没有意义。



关系和属性不会被处理相同,即使它们都返回对象。关系是指向外部对象的指针,因此如果在关系的另一端没有设置对象,则可以返回 nil 。属性是仅由对象持有的 ,因此如果未设置,则总是由 [NSNull null]



默认情况下,所有带数值的属性都会返回一个初始化为零的NSNumber对象。如果你删除默认值,你会得到 [NSNull null]



NSNull null] 是一个单例,你可以使用简单的指针比较来检查它

  if (myMo.numericalAttribute == [NSNull null])... 


I'm having some trouble with Managed Objects... imagine that.

Here is one real doozy, maybe someone can help.

So I have a managed object class called "Format" which is an entity. Anyway, it has a property called "slot" that's an NSNumber. Now the number can have values from zero to four, but if it does not have a value then I want the NSNumber object to be equal to "nil" as opposed to zero. I wasn't having any luck with that since evidently being zero is the same as being "nil." Ugh. (Is there a way to tell if the pointer is simply empty, "nil," as opposed to pointing to a zero value?)

At any rate, I tried a work-around which was to render the variable into text like so:

if(@"myFormat.slot" != @"(null)")
    NSLog(@"slot==%@",myFormat.slot);

But the problem is that I got this in my log:

slot==(null)

So, OK... what the heck? If @"myFormat.slot" == @"(null)" then how the heck is that if statement resolving...?!?!

I'm truly baffled now... please someone help me out.

解决方案

You won't ever get a nil back from an attribute. Instead, you get a [NSNull null] object.

In Objective-C, nil and null are not interchangeable. When you see nil you are almost looking at a dereferenced pointer. nil is intended to convey that no object has been assigned to the symbol. null by contrast is the singleton instance of [NSNull null]. It is used as a placeholder to indicate that some value, represented by an object, has not been set. In other words, a value of nil doesn't make sense in Objective-C.

In Core Data, relationships and attributes are not treated the same even though they both return objects. A relationship is a pointer to an external object and therefore can have a return nil if no object has been set on the other side of the relationship. An attribute is a value only held by an object and therefore is always represented by [NSNull null] if not set.

By default, all attributes with numerical value will return an NSNumber object initialized to zero. If you remove the default you get [NSNull null].

However, since [NSNull null] is a singleton you can use a simple pointer comparison to check for it e.g.

if (myMo.numericalAttribute == [NSNull null])...

However, that is considered bad practice.

这篇关于if语句失败,使用CoreData受管对象的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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