Ok here goes my situation:
I have a core-data generated class Quantity which has one of its attributes ‘Value’, marked as a float.
There’s a convenience method in the Quantity class which helps me set this property:
- (void)setValueValue:(float)value_ {
[self setValue:[NSNumber numberWithFloat:value_]];
}
Now when I set try set this value from a Controller like this,
Quantity *q = (Quantity *) [NSEntityDescription insertNewObjectForEntityForName:@"Quantity" inManagedObjectContext:self.managedObjectContext];
float qv = 100.0f;
[q setValueValue:qv];
the value gets set as 1.40129846e-43
I put breakpoints in both locations above, and while the tooltip correctly shows the value as 100.0 in the Controller class, it shows the incorrect value in the setter method.
And ultimately, the value that gets stored in the object and the db is the incorrect value.
Any clues as to what’s happening here??
1.40129846e-43is a very interesting number, and tells us what’s happening here.Specifically:
More simply, the value that you’re observing is what you get if you interpret the integer value
100as the encoding of afloat.What is most likely happening is that you haven’t included the header that declares the
setValueValue:(float)method, or that the declaration is otherwise not in scope at the location from which you are calling the method. This results in the compiler assuming that the method takes an integer argument instead of a floating-point argument.Thus, the caller converts your
100.fvalue into the integer100, which is then interpreted by the function as the encoding of the floating-point number you are observing.The other possibility is that a similar type impedance mismatch is happening somewhere in your logging code.
TLDR: make sure you declare your functions before you use them in C-based languages.