How to check that float property of managed object is nil?
Score is NSManagedObject. score.latit开发者_JAVA百科ude is an NSNumber, which actually is a float value.
NSLog(@"%f", [score.latitude floatValue]);
shows 0.000000.
Construction I use to check for nil:
if (score.latitude == nil)
doesn't work.
It's irrelevant that there is a floating-point value stored in your NSNumber
instance; the object itself is either nil
or not. To check for an object being nil
, if( obj == nil )
is functionally equivalent to if( obj )
.
So your conditional may not be doing what you expect, but it is checking for nil
. You'd need to add more details about what's happening in the body of the if
, should you need more specific explanation.
The key point is that sending a message to nil
, such as [nil floatValue]
always returns 0
, interpreted as whatever the return type of the method is. For example, if you send floatValue
to nil
, you'll get back floating-point 0. If you send a message that should return an object, you'll get nil
; a message that should return an int
, you get integer 0.
Also be aware that when you say if( score.latitude )
, the expression will evaluate false if either score
or latitude
is nil
(because if score
is nil
, sending latitude
to it will return nil
.)
A float can be nil, as it isn't an object.
And a NSNumber is not an float value, but has a method, that returns a float value.
use if (score)
to check, if the NSNumber got instantiated before.
As I finally understand, the problem is because of Core Data save data to sql database, which stores nil properties as 0.000...
精彩评论