本文介绍了iOS转化器应用程序,度量之间的联系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以帮助我建立度量之间的联系,例如,如何计算厘米和英寸之间的关系,如图所示。这是我用于学校项目的应用,除那部分外,它或多或少都完成了。所以我正在从plist读取值,并根据我在选择器中的第一列显示了第二和第三。所有单位和度量均从plist中读取。以能量,距离,体积等为键。

I am wondering if someone could help me how to make connections between measures, for example how can I calculate relation between centimeters and inches like in the picture. This is my app for school project and it is more or less done except that part. So i am reading values from plist, and based on my first column in picker, second and third are displayed. All units and measures are read from plist. With energy, distance, volume etc. as keys.

那么在度量之间建立联系的最简单方法是什么?我最简单也是最业余的想法是编写loooong代码,如下所示:

So what would be easiest way to make connections between measures? My easiest and most amateur idea is to write loooong code, something like this:

if (first column == distance){
    if (second column ==millimeter) {
    case 0: if (third column == millimeter) then result=x*1;
    case 1:if (third column == centimeter) then result=0/100;
      .
      .
      .
    }
    if (second column == centimeter){
    case 0:
    case 1:
      .
      .
      .
    }}

这将导致1000行不必要的代码。有谁有更好的主意?

and that would result in like 1000 lines of unnecessary code. Does anyone have better idea? Thanks in advance.

推荐答案

整个事情都可以用数据完成,没有,没有开关 ...就可以了...有一点组织。考虑以下内容:

The whole thing can be done with data, no ifs, no switches... it just needs to be organized a little bit. Consider the following:

@property (strong, nonatomic) NSArray *distanceIndex;
@property (strong, nonatomic) NSArray *weightIndex;

@property (strong, nonatomic) NSArray *distanceConversion;
@property (strong, nonatomic) NSArray *weightConversion;

- (void)initConversions {

    // we get strings in as our units, so we need to convert these to array indexes
    // make up our own convention, meters=0, inches=1, etc

    self.distanceIndex = @[@"meters", @"inches", @"miles"];
    self.weightIndex = @[@"grams", @"ounces", @"pounds"];

    // you might read these from a file, but in code, just to illustrate...
    // the order follows the order of the index.  so the meters line is first
    // and the conversion factors are meters->meters, meters->inches, meters->miles etc.
    // notice the 1.0's on the diagonal, and that numbers reflected across
    // the diagonal are reciprocals

    self.distanceConversion =
        @[ @[@"meters", @[@1.0, @39.37, @0.000621]],
           @[@"inches", @[@0.254, @1.0, @0.00001578]],
           @[@"miles ", @[@1609.0, @63360.0, @1.0]] ];

   // and so on... same thing for self.weightConversions
}

- (double)convertDistance:(double)value from:(NSString *)fromUnits to:(NSString *)toUnits {

    NSUInteger fromIndex = [self.distanceIndex indexOfObject:fromUnits];
    NSUInteger toIndex = [self.distanceIndex indexOfObject:toUnits];

    // throw an exception if passed units that we don't recognize
    NSAssert(fromIndex != NSNotFound && toIndex != NSNotFound, @"could not find units");

    NSArray *conversionRow = self.distanceConversion[fromIndex][1];
    NSNumber *conversionNumber = conversionRow[toIndex];
    double conversion = [conversionNumber doubleValue];

    return value * conversion;
}
- (double)convertWeight:(double)value from:(NSString *)fromUnits to:(NSString *)toUnits {

    // same idea here, if you have the weight conversion data
    return 0;
}

这篇关于iOS转化器应用程序,度量之间的联系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 12:53