Coredata浮点数变为零
我有一个无法用核心数据实体解决的问题。
在我的实体中有两个关系(whoHasToPay、whoHasToBePaid)和一个属性(howMuch)。
这是我在数据库中插入记录的代码
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];
NSLog 显示,
<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>";
})
因此值 howMuch
似乎是正确的。
再次调用它时
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"]);
NSLog(@"%@", [[dc valueForKey:@"whoHasToPay"] name]);
NSLog(@"%@", [[dc valueForKey:@"whoHasToBePaid"] name]);
}
[fetchRequestCount release];
但是,当我使用NSLog
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
,显示howMuch
似乎已变为零。为什么?我错了吗?
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"]);
NSLog(@"%@", [[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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个日志错误。浮点数和其他数值存储为 NSNumber 对象。这:
...返回一个 NSNumber 对象而不是浮点值。您需要将日志更改为:
...并且您应该看到正确的值。
It is a logging error. Floats and other numerical values are stored as NSNumber objects. This:
... returns an NSNumber object and not a float value. You need to change the log to:
... and you should see the correct value.