gpt4 book ai didi

iphone - 从核心数据中获取时, float 变为 0.0000

转载 作者:行者123 更新时间:2023-11-28 22:57:06 25 4
gpt4 key购买 nike

我创建了一个带有 float 属性的实体并将其值设置为 0.5

+(void)createSportNamed:(NSString *)name withContext:(NSManagedObjectContext *)context{
Sport *newSport = [NSEntityDescription insertNewObjectForEntityForName:@"Sport" inManagedObjectContext:context];
newSport.name = name;
newSport.maxRep = [NSNumber numberWithInt:45];
newSport.endurance = [NSNumber numberWithFloat:0.5f];
NSLog(@"endurance at set = %f",[newSport.endurance floatValue]);

NSError *saveError = nil;
[context save:&saveError];
}

从log来看,float的值还是0.5000

但是当我稍后获取它时,值不知何故变成了 0.0000

-(NSArray*)createWorkoutForSport:(NSString*)sportName withContext:(NSManagedObjectContext*)context{

NSFetchRequest *request = [NSFetchRequest new];
request.entity = [NSEntityDescription entityForName:@"Sport" inManagedObjectContext:context];
NSPredicate *sportNamePredicate = [NSPredicate predicateWithFormat:@"name == %d",sportName];
request.predicate = sportNamePredicate;
NSError *err = nil;
NSArray *results = [context executeFetchRequest:request error:&err];
Sport *theFetchedSport = [results lastObject];

int totalRep = [[theFetchedSport maxRep]intValue];
float endure = [[theFetchedSport endurance]floatValue];
int set;
NSLog(@"Endurance = %f",endure);

最佳答案

+(void)createSportNamed:(NSString *)name withContext:(NSManagedObjectContext *)context{
Sport *newSport = [NSEntityDescription insertNewObjectForEntityForName:@"Sport" inManagedObjectContext:context];
newSport.name = name;

这清楚地表明您的 Sport 实体的“名称”字段是一个字符串。

[NSPredicate predicateWithFormat:@"name == %d",sportName]

然而在这里你使用的是 %d,这意味着 int。您正在将字符串指针转换为 int。您应该使用 %@。因此,您的提取请求返回一个空数组,-lastObject 返回 nil,这意味着 [nil endurance] 也是 nil,这意味着 [[nil endurance] floatValue] 也是 nil,即 0

关于iphone - 从核心数据中获取时, float 变为 0.0000,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10568966/

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