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
nilback from an attribute. Instead, you get a[NSNull null]object.In Objective-C,
nilandnullare not interchangeable. When you seenilyou are almost looking at a dereferenced pointer.nilis intended to convey that no object has been assigned to the symbol.nullby 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 ofnildoesn’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
nilif 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.However, that is considered bad practice.