4 回答
The problem occurs because you use pointers in NSInteger
and float
. I have modified your method with the implementation for both cm and inches, as follows:
- (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;
}
Some Test:
[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"];
The output:
result: 180.34 cm
result: 5’11”
result: 27.94 cm
result: 0’11”
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)];
So in short you need to know the difference between native/primitive type's and reference types (type that use pointers this character --> *. This might help Link
After some testings from the last answer(from tolgamorf) here is the final version. To me it works perfectly.
- (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;
}
For today we have useful API from HealthKit:
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)
Just copy to Playground and play converting of values from cm to inches and back. The formatter configuration:
let h_formatter = LengthFormatter()
h_formatter.isForPersonHeightUse = true
h_formatter.unitStyle = .short
In the result we have:
180cm -> "5′ 10.866″" and back to 180cm
I hope it'll help.