2016-10-09 204 views
0

我正在進行簡單的轉換(例如,英制到公制)。我用(String,Closure)元素創建了一個元組數組。這可以讓我爲轉換(例如「英里到公里」)轉換爲一個選擇器控件,並在一行中引用相關的公式,並將值傳遞給轉換。我遇到的問題是當我使用$ 0 。關閉速記我得到一個錯誤「表達式太複雜,無法在合理的時間來解決。」速記中的閉包數組:Xcode Swift:表達式太複雜,無法在合理的時間內解決

這裏的聲明&代碼工作:

低於初始類聲明的ViewController性能聲明:

var formulaTuple = [(convString: String, convFormula: ((Double) -> Double))]() 

分配在viewDidLoad()

formulaTuple = [("miles to kilometers", {(a: Double) -> Double in return (a/0.62137) }), 
       ("kilometers to miles", {(a: Double) -> Double in return (a * 0.62137) }), 
       ("feet to meters", {(a: Double) -> Double in return (a/3.2808) }), 
       ("yards to meters", {(a: Double) -> Double in return (a/1.0936) }), 
       ("meters to feet", {(a: Double) -> Double in return (a * 3.2808) }), 
       ("meters to yards", {(a: Double) -> Double in return (a * 1.0936) }), 
       ("inches to centimeters", {(a: Double) -> Double in return (a/0.39370) }), 
       ("centimeters to inches", {(a: Double) -> Double in return (a * 0.39370) }), 
       ("fahrenheit to celsius", {(a: Double) -> Double in return ((a - 32) * (5/9)) }), 
       ("celsius to fahrenheit", {(a: Double) -> Double in return (a * (9/5) + 32) }), 
       ("quarts to liters", {(a: Double) -> Double in return (a/1.05669) }), 
       ("liters to quarts", {(a: Double) -> Double in return (a * 1.05669) }) ] 

而且在代碼中工作調用,其中行是選擇器中被點擊的行,inputValue是要傳遞的內容,outputValue是轉換的結果。

outputValue = formulaTuple [行] .convFormula(inputValue的)當我嘗試使用此語法在viewDidLoad中(修剪下來的聲明

出現問題),而不是上面的synatax:

formulaTuple = [("miles to kilometers", {$0/0.62137 }), 
        ("kilometers to miles", {$0 * 0.62137 }), 
        ("feet to meters", {$0/3.2808 }), 
        ("yards to meters", {$0/1.0936 }), 
        ("meters to feet", {$0 * 3.2808 }), 
        ("meters to yards", {$0 * 1.0936 }), 
        ("inches to centimeters", {$0/0.39370}), 
        ("centimeters to inches", {$0 * 0.39370 }), 
        ("fahrenheit to celsius", {($0 - 32) * (5/9) }), 
        ("celsius to fahrenheit", {$0 * (9/5) + 32 }), 
        ("quarts to liters", {$0/1.05669 }), 
        ("liters to quarts", {$0 * 1.05669 }) ] 

我認爲這會更順暢,但它似乎打破了Xcode。思考?我的方法基本上是不健全的,會推薦一個不同的方法? 謝謝!

回答

2

當你給它大數組文字,然後期望它解釋類型時,Swift不會很好。在你的情況下,我預料到這是可行的,因爲你的formulaTuple屬性已經有了一個確定的類型。

作爲變通,可以先初始化一個常量數組,然後將其分配給您的財產:

let temp: [(String, (Double) -> Double)] = [ 
      ("miles to kilometers", {$0/0.62137 }), 
      ("kilometers to miles", {$0 * 0.62137 }), 
      ("feet to meters", {$0/3.2808 }), 
      ("yards to meters", {$0/1.0936 }), 
      ("meters to feet", {$0 * 3.2808 }), 
      ("meters to yards", {$0 * 1.0936 }), 
      ("inches to centimeters", {$0/0.39370}), 
      ("centimeters to inches", {$0 * 0.39370 }), 
      ("fahrenheit to celsius", {($0 - 32) * (5/9) }), 
      ("celsius to fahrenheit", {$0 * (9/5) + 32 }), 
      ("quarts to liters", {$0/1.05669 }), 
      ("liters to quarts", {$0 * 1.05669 }) 
] 

formulaTuple = temp 

備選答案

元組的真正含義用於臨時存儲和從一個函數傳回多個結果。您可能要考慮使用struct爲您的數組的值類型:

struct Conversion { 
    let string: String 
    let formula: (Double) -> Double 
} 

var conversions = [Conversion]() 

conversions = [ 
    Conversion(string: "miles to kilometers", formula: {$0/0.62137 }), 
    Conversion(string: "kilometers to miles", formula: {$0 * 0.62137 }), 
    Conversion(string: "feet to meters", formula: {$0/3.2808 }), 
    Conversion(string: "yards to meters", formula: {$0/1.0936 }), 
    Conversion(string: "meters to feet", formula: {$0 * 3.2808 }), 
    Conversion(string: "meters to yards", formula: {$0 * 1.0936 }), 
    Conversion(string: "inches to centimeters", formula: {$0/0.39370}), 
    Conversion(string: "centimeters to inches", formula: {$0 * 0.39370 }), 
    Conversion(string: "fahrenheit to celsius", formula: {($0 - 32) * (5/9) }), 
    Conversion(string: "celsius to fahrenheit", formula: {$0 * (9/5) + 32 }), 
    Conversion(string: "quarts to liters", formula: {$0/1.05669 }), 
    Conversion(string: "liters to quarts", formula: {$0 * 1.05669 }) 
] 

outputValue = conversions[row].formula(inputValue) 

雨燕是該高興多了,該temp解決辦法是沒有必要的。

+1

該結構工作得很好。我重讀了Tuples上的Swift文檔,並更加關注了有關臨時類型的一點。真的很感謝你的建議,見解和背景。謝謝! – Gallaugher

相關問題