2014-01-24 41 views
0

我在想如果有人能幫助我如何使措施之間的聯繫,例如我怎樣才能計算釐米和英寸之間的關係,如圖片。這是我的學校項目應用程序,除了那個部分之外,它或多或少都已經完成。所以我正在閱讀plist的值,並根據我的第一列選擇器顯示第二和第三列。所有的單位和措施都是從plist中讀取的。以能量,距離,音量等爲關鍵。iOS轉換器應用程序,措施之間的聯繫

那麼什麼是使措施之間的連接最簡單的方法?我最容易和最業餘的想法是編寫loooong代碼,如下所示:

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行不必要的代碼。有沒有人有更好的主意?提前致謝。

回答

1

整個事情可以用數據完成,沒有if s,沒有switch es ......它只是需要組織一點點。考慮以下幾點:

@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; 
} 
+0

謝謝!很有幫助。 – user3194296

1

如果我寫這一點,我這樣做:

對於每種類型的轉換(我要使用距離作爲例子) 首先選擇一個標準單元(I米採摘表) 現在把每個單位轉換爲米,並將其保存到數據庫(或陣列,plist,無論)。

例在每平方釐米:0.1公里:1000等

然後爲每個需要做,只取兩者的轉換次數,使用第一個輸入轉換爲米,並轉換爲轉換使用第二個輸出。例如:5公分到5公里,5 * 0.1 = 0.5公尺。 0.5/1000 = 0.0005公里。

這就是我將如何處理這個問題

+0

謝謝,這是一個很好的方法,但我怎麼看待使用哪種措施?再次,我會需要很多如果和案例,看看寫在第二和第三列 – user3194296

1

您可能想採取面向對象的方法。

有一個基本的抽象類或接口,它可以包含兩個稱爲UnitType的單元。 它將具有2個屬性FromUnitToUnit,和被叫Convert:方法。

也有另一個名爲單位抽象類。 這將有兩種方法ConvertToStanard:amountConvertFromStandard:amount 這將轉換爲每種類型的標準單位,說最低單位。

所以你MeterUnit看起來像這樣

@implementation MeterUnit 

-(double) ConvertToStandard:(double) amount 
{ 
    return amount/100; 
} 

-(double) ConvertFromStandard:(double) amount 
{ 
    return amount*100; 
} 

@end 

和你釐米單位將只返回量(它已經以釐米爲單位)。現在

@implementation CentimeterUnit 

-(double) ConvertToStandard:(double) amount 
{ 
    return amount; 
} 

-(double) ConvertFromStandard(double) amount 
{ 
    return amount; 
} 

@end 

您UNITTYPE的轉換()方法只是看起來是這樣,因爲每個子類定義熱去/從標準單位,您DistanceUnitType類並不知道也不關心它是如何做。它只知道調用ConvertToStandard和ConvertFromStandard :.

-(double) Convert:(double) amount 
{ 
    double stanardUnit = [FromUnit ConvertToStandard:amount]; 
    return [ToUnit ConvertFromStandard:standardUnit]; 
} 

UnitType也可能定義可用的單位並將其提供給UIPicker。

事實上,你可以在每個單元的類上只有兩個屬性,並且只需要你的基類調用標準函數。

爲什麼這更好?也許不是,但至少你只是定義每個單位一次。在嵌套的if/case語句中,您需要定義每個轉換很多次。另外如果你想添加一個新的,你需要在switch語句中創建一堆新條目。但在這種情況下,您只需定義一個類並將其添加爲UnitType的可用單位。而已!無論如何,我確信有很多錯誤和問題,但如果這是一個類項目,它可能很好,如果是這樣的話。希望它能提供一個解決這個問題的方法。