我对objective-c很新。
我正在尝试创建一个需要英寸或厘米的函数,并将值转换为oposite公制系统。
鉴于我不知道如何相应地设置变量,我目前正在努力研究如何在objective-c中进行数学方程式。因为我总是从XCode得到一些错误,说变量是一个int但不应该是指针是错误的和其他东西......基本...我不知道如何制作数学......
目的是我传递存储在其他地方的字符串5’11”
或1.80 cm
并将其转换为相反的公制系统。
下面的代码尝试将英寸转换为cm ...如果我们传递5'5“,则数学等式应该是((5 * 12) + 3) * 2.54
这等于180.34
并且我看起来的格式化结果函数是1.80 cm
以下代码是我得到的,但它不适合我。如果有人能告诉我如何在我的代码中执行此操作,我将不胜感激。
- (NSString *)returnRightMetric:(NSString *)theMeasure typeOfMetricUsed:(NSString *)metricType
{
if ([metricType isEqualToString:@"inches"]) {
NSArray* theConvertion = [theMeasure componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@"’”"]];
NSInteger* value1 = [theConvertion[0] intValue];
NSInteger* value2 = [theConvertion[1] intValue];
float *number = ((value1 * 12) + value2) * 2.54;
///.....
} else if ([metricType isEqualToString:@"cm"])
{
}
return result;
}
答案 0 :(得分:3)
出现此问题的原因是您使用NSInteger
和float
中的指针。我已经使用cm和inches的实现修改了你的方法,如下所示:
- (NSString *)returnRightMetric:(NSString *)theMeasure typeOfMetricUsed:(NSString *)metricType
{
NSString *result = nil;
if ([metricType isEqualToString:@"inches"]) {
NSArray* theConvertion = [theMeasure componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@"’”"]];
NSInteger value1 = [theConvertion[0] intValue];
NSInteger value2 = [theConvertion[1] intValue];
float number = ((value1 * 12) + value2) * 2.54;
result = [NSString stringWithFormat:@"%.02f cm", round(number * 100.0) / 100.0];
} else if ([metricType isEqualToString:@"cm"]) {
float value = [theMeasure floatValue];
float number = value / 2.54;
if (number > 12.0) {
result = [NSString stringWithFormat:@"%i’%i”", (int)floor(number / 12.0), (int)number % 12];
} else {
result = [NSString stringWithFormat:@"0’%i”", (int)round(number)];
}
}
NSLog(@"result: %@", result);
return result;
}
一些测试:
[self returnRightMetric:@"5’11”" typeOfMetricUsed:@"inches"];
[self returnRightMetric:@"180.34 cm" typeOfMetricUsed:@"cm"];
[self returnRightMetric:@"0’11”" typeOfMetricUsed:@"inches"];
[self returnRightMetric:@"27.94 cm" typeOfMetricUsed:@"cm"];
输出:
result: 180.34 cm
result: 5’11”
result: 27.94 cm
result: 0’11”
答案 1 :(得分:2)
NSArray* theConvertion = [theMeasure componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@"’”"]];
int value1 = [theConvertion[0] intValue];
int value2 = [theConvertion[1] intValue];
float number = ((value1 * 12) + value2) * 2.54;
NSString *formattedNumber = [NSString stringWithFormat:@"%.02f", (number/100)];
简而言之,你需要知道原生/原始类型和引用类型之间的区别(使用指针这个字符的类型 - > *。这可能有助于Link
答案 2 :(得分:1)
从最后一个答案(来自tolgamorf)进行一些测试后,这里是最终版本。对我来说它完美无缺。
- (NSString *)returnRightMetric:(NSString *)theMeasure typeOfMetricUsed:(NSString *)metricType
{
NSString *result = nil;
if ([metricType isEqualToString:@"inches"]) {
if ([theMeasure isEqualToString:@""]) {
return @"0";
}
NSArray* theConvertion = [theMeasure componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@"’”"]];
NSInteger value1 = [theConvertion[0] intValue];
NSInteger value2 = [theConvertion[1] intValue];
float number = ((value1 * 12) + value2) * 2.54;
result = [NSString stringWithFormat:@"%.0f cm", round(number * 100.0) / 100.0];
} else if ([metricType isEqualToString:@"cm"]) {
float value = [theMeasure floatValue];
float number = value / 2.54;
if (roundf( number) >= 12.0) {
if ((int)round( number) % 12==0) {
result = [NSString stringWithFormat:@"%i’%i”", (int)roundf(number / 12.0), (int)round( number) % 12];
}else{
result = [NSString stringWithFormat:@"%i’%i”", (int)floorf(number / 12.0), (int)round( number) % 12];
}
} else {
result = [NSString stringWithFormat:@"0’%i”", (int)round(number)];
}
}
NSLog(@"result: %@", result);
return result;
}
答案 3 :(得分:1)
今天我们有来自HealthKit的有用API:
let cm_q = HKQuantity(unit: HKUnit(from: .centimeter), doubleValue: 180)
var inch_string = h_formatter.string(fromMeters: cm_q.doubleValue(for: HKUnit.meter()))
var inches_double = cm_q.doubleValue(for: HKUnit.inch())
let inch_q = HKQuantity(unit: HKUnit(from: .inch), doubleValue: inches_double)
var cm_d = inch_q.doubleValue(for: HKUnit(from: .centimeter))
var string = h_formatter.string(fromValue: cm_d, unit: .centimeter)
只需复制到Playground并播放值从cm转换为英寸并返回。格式化程序配置:
let h_formatter = LengthFormatter()
h_formatter.isForPersonHeightUse = true
h_formatter.unitStyle = .short
在结果中我们有:
180cm - > “5'10.866”“并回到180cm
我希望它会有所帮助。