2017-01-05 6 views
0

私は、エラーを受信して​​います:
エラー受信:「タイプのインデックスを持つ型の値を添字できません 『[ダブル]を』 '(任意) - > int型」

“Cannot subscript a value of type ‘[Double]’ with an index of type ‘(Any) -> Int’” on the following line of code:

tipPer = tipPercentages[index] 
ここ

は(誤ったラインを含む)私のコードの残りの部分である:((ひどいフォーマット/構文については申し訳ありませんが、私はスウィフトに新たなんだ)!)

import UIKit 

class ViewController: UIViewController { 

    var tipPer: Int = 0 

    let defaults = UserDefaults.standard 

    @IBOutlet weak var tipLabel: UILabel! 
    @IBOutlet weak var totalLabel: UILabel! 
    @IBOutlet weak var perPersonLabel: UILabel! 
    @IBOutlet weak var billField: UITextField! 
    @IBOutlet weak var peopleField: UITextField! 
    @IBOutlet weak var tipControl: UISegmentedControl! 


    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(animated) 

     // This is a good place to retrieve the default tip percentage from UserDefaults 
     // and use it to update the tip amount 
    } 

    override func viewDidLoad() { 

     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    @IBAction func onTap(_ sender: AnyObject) { 

     view.endEditing(true) 
    } 


    @IBAction func calculateTip(_ sender: AnyObject) { 

     let tipPercentages = [0.10, 0.15, 0.20] 

     defaults.synchronize() 



     if (tipControl.selectedSegmentIndex == 0) { 
      let index = tipControl.selectedSegmentIndex 
     } 

     if (tipControl.selectedSegmentIndex == 1) { 
      let index = tipControl.selectedSegmentIndex 
     } 

     if (tipControl.selectedSegmentIndex == 2) { 
      let index = tipControl.selectedSegmentIndex 
     } 

     if (tipControl.selectedSegmentIndex == 3) { 

      let index = defaults.integer(forKey: "defaultTipControlKey") 
     } 


     let tipPer = tipPercentages[index] 
     let bill = Double(billField.text!) ?? 0 
     let people = Double(peopleField.text!) ?? 1 

     let tip = bill * tipPer 
     let total = bill + tip 
     let perPerson = total/people 

     tipLabel.text = String(format: "$%.2f", tip) 
     totalLabel.text = String(format: "$%.2f", total) 
     perPersonLabel.text = String(format: "$%.2f Each", perPerson) 

    } 
} 

私はそれがで何かを持っていることを知っていますインデックス変数を整数に設定するh私はやろうとしましたが、何かが欠けています。どんな助けもありがとう。

+0

どのように 'index'を使用していますか?すべての 'index'は' if {'文の中で定義されていますか? 'index'は' tipPercentages [index] 'のなにか他のものです。 –

+0

問題を解決できましたか? –

答えて

1

問題は、あなたがそれはあなたのためif block後には使用できませんのでので、その範囲は、そのif blockに対応しているすべてのif blockの中で宣言していることindex一定です。この問題を解決するには、for ifブロックの前にindex変数を宣言し、複数のifブロックを作成する代わりに、このような単一のif conditionが必要です。

var index = defaults.integer(forKey: "defaultTipControlKey") //Default value 
if (tipControl.selectedSegmentIndex != 3) { 
    index = tipControl.selectedSegmentIndex 
} 
let tipPer = tipPercentages[index] 

注:あなたif conditionあなたは、単にインデックスを取得するために1つのif blockの上に好きな意味がありません。

+0

編集した回答を確認してください –

関連する問題