Coredata Float number becomes zero
I have a problem I cannot solve with a Core Data entity.
In my entity there are two relations (whoHasToPay, whoHasToBePaid) and an attribute (howMuch).
This is the code in which I insert the record in the database
Debitcredit *dc = [NSEntityDescription
insertNewObjectForEntityForName:@"Debitcredit"
inManagedObjectContext:self.context];
dc.howMuch = [NSNumber numberWithFloat:5.2f];
dc.whoHasToPay = theDebitor;
dc.whoHasToBePaid = theCreditor;
NSLog(@"%@", dc);
[self.context save:&error];
The NSLog shows
<Debitcredit: 0x151590> (entity: Debitcredit; id: 0x141570
<x-coredata:///Debitcredit/t8DC4691F-5DE3-42D5-8095-C2D5D3264C8D2> ; data: {
howMuch = "5.2";
whoHasToBePaid = "0x140c50 <x-coredata://C25DC4FE-B46B-402C-B513-A2A83C6A9F86/Users/p1>";
whoHasToPay = "0x14c280 <x-coredata://C25DC4FE-B46B-402C-B513-A2A83C6A9F86/Users/p2>";
})
so the value howMuch
seems to be correct.
However, when I call it again with
fetchRequestCount = [[NSFetchRequest alloc] init];
entityCount = [NSEntityDescription entityForName:@"Debitcredit"
inManagedObjectContext:self.context];
[fetchRequestCount setEntity:entityCount];
fetchedObjectsCount = [self.context executeFetchRequest:fetchRequestCount error:&error];
for (Debitcredit *dc in fetchedObjectsCount) {
NSLog(@"%f", [dc valueForKey:@"howMuch"]);
NSL开发者_如何转开发og(@"%@", [[dc valueForKey:@"whoHasToPay"] name]);
NSLog(@"%@", [[dc valueForKey:@"whoHasToBePaid"] name]);
}
[fetchRequestCount release];
the NSLog shows
2010-12-05 01:47:04.636 myApp[6179:307] 0.000000
2010-12-05 01:47:04.642 myApp[6179:307] John
2010-12-05 01:47:04.646 myApp[6179:307] Jack
It seems that howMuch
has become zero. Why? Am I wrong?
It is a logging error. Floats and other numerical values are stored as NSNumber objects. This:
[dc valueForKey:@"howMuch"]
... returns an NSNumber object and not a float value. You need to change the log to:
NSLog(@"%@", [dc valueForKey:@"howMuch"]);
... and you should see the correct value.
精彩评论