I created an entity with a float attribute and set it’s value to 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];
}
From the log, the value of the float is still 0.5000
But when I fetch it later on, the value somehow became 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);
This clearly shows that the “name” field of your Sport entity is a string.
And yet here you are using
%d, which meansint. You’re casting a string pointer to an int. You should be using%@. Thus, your fetch request is returning an empty array, and-lastObjectis returningnil, which means that[nil endurance]is alsonil, which means that[[nil endurance] floatValue]is alsonil, or0.