gpt4 book ai didi

ios - Coredata float 变为零

转载 作者:行者123 更新时间:2023-11-28 20:47:34 27 4
gpt4 key购买 nike

我有一个无法用 Core Data 实体解决的问题。

在我的实体中有两个关系(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 似乎变成了零。为什么?我错了吗?

最佳答案

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

[dc valueForKey:@"howMuch"]

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

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

...您应该会看到正确的值。

关于ios - Coredata float 变为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4356757/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com