Coredata浮点数变为零

发布于 2024-10-06 01:42:40 字数 1693 浏览 2 评论 0原文

我有一个无法用核心数据实体解决的问题。

在我的实体中有两个关系(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔戏命师 2024-10-13 01:42:40

这是一个日志错误。浮点数和其他数值存储为 NSNumber 对象。这:

[dc valueForKey:@"howMuch"]

...返回一个 NSNumber 对象而不是浮点值。您需要将日志更改为:

NSLog(@"%@", [dc valueForKey:@"howMuch"]);

...并且您应该看到正确的值。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文