gpt4 book ai didi

iphone - 关于单位换算的简单问题

转载 作者:行者123 更新时间:2023-12-03 21:06:47 25 4
gpt4 key购买 nike

我使用以下代码在表格中显示我的数据的千克与磅。

由于某种原因,它无法正常工作,并且给了我巨大的数字。有什么想法吗?

我正在使用 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;
}

最佳答案

哦,这个问题很容易解决。 weightInKilosweightInPoundsNSNumber 实例。当您在格式中使用 %i 格式化程序时,这意味着您正在提供一个整数。您最终提供的整数是每个对象的指针值。由于您需要每个 NSNumber 的字符串值,因此请使用实例方法 -stringValue

这是基于这些更改的代码的更新版本。

- (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];
}

请记住,处理对象时必须遵循内存管理规则。当您使用 -init... 方法创建对象时,必须确保释放它。在您的代码中,这意味着 weightTextcellText

PS:DDUnitConverter 是一个很棒的发现。如果我在未来的项目中需要它,我必须将其添加为书签。

关于iphone - 关于单位换算的简单问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6350426/

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