Reputation:
I am using the following code to display kg vs lbs in for my data in a table.
For some reason it is not working correctly and is giving me huge numbers. Any ideas?
I am using DDUnitConverter (https://github.com/davedelong/DDUnitConverter)
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSString *repsText = [[managedObject valueForKey:@"reps"] description];
NSNumber *weightInPounds = [NSNumber numberWithFloat:[[managedObject valueForKey:@"weight"]floatValue]];
NSNumber *weightInKilos = [[DDUnitConverter massUnitConverter] convertNumber:weightInPounds fromUnit:DDMassUnitUSPounds toUnit:DDMassUnitKilograms];
NSString *weightText = nil;
NSString *cellText = nil;
BOOL isKgs = [[NSUserDefaults standardUserDefaults] boolForKey:@"wantsKGs"];
if (isKgs == 1)
{
weightText = [[NSString alloc]initWithFormat:@"%i", weightInKilos];
cellText = [[NSString alloc]initWithFormat:@"Set %i: %@ reps at %@ kgs",indexPath.row + 1, repsText, weightText];
}
else
{
weightText = [[NSString alloc]initWithFormat:@"%i", weightInPounds];
cellText = [[NSString alloc]initWithFormat:@"Set %i: %@ reps at %@ lbs",indexPath.row + 1, repsText, weightText];
}
cell.textLabel.text = cellText;
}
Upvotes: 2
Views: 277
Reputation: 16024
Oh, this is an easy problem to solve. weightInKilos
and weightInPounds
are NSNumber
instances. When you use the %i
formatter in formats, it means you are supplying an integer. The integer you end up supplying is the pointer value to each object. Since you want the string value of each NSNumber, use the instance method -stringValue
.
Here's an updated version of your code based on those changes.
- (void) configureCell: (UITableViewCell *) cell atIndexPath: (NSIndexPath *) indexPath
{
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath: indexPath];
NSString *repsText = [[managedObject valueForKey: @"reps"] description];
NSNumber *weightInPounds = [NSNumber numberWithFloat: [[managedObject valueForKey: @"weight"] floatValue]];
// Alternatively you could just use ‘[managedObject valueForKey: @"weight"]’ if the ‘weight’ attribute is a number.
NSNumber *weightInKilos = [[DDUnitConverter massUnitConverter] convertNumber: weightInPounds
fromUnit: DDMassUnitUSPounds
toUnit: DDMassUnitKilograms];
BOOL isKgs = [[NSUserDefaults standardUserDefaults] boolForKey:@"wantsKGs"];
NSString *weightText = (isKgs ? [weightInKilos stringValue] : [weightInPounds stringValue]);
cell.textLabel.text = [NSString stringWithFormat: @"Set %i: %@ reps at %@ lbs",indexPath.row + 1, repsText, weightText];
}
Remember that you have to follow the memory management rules when dealing with objects. When you create an object using an -init...
method, you have to be sure to release it. In your code that means weightText
and cellText
.
PS: DDUnitConverter is a great find. I'll have to keep it bookmarked if I ever need it in future projects.
Upvotes: 2