变量赋值错误的声明

时间:2019-05-14 10:35:08

标签: swift xcode variables

我有一组用IBAction func编写的代码。我将需要声明一个var,但是我不确定要为表达式添加什么内容。

我尝试将其声明为Double,Int,String等。以及一个像这样的空字符串var num = ""

这是函数中的代码

@IBAction func btnAdd(_ sender: UIButton) {
        num = Double(txtInput.text!)
           guard let score = Double(txtInput.text!), score != nil else {
            txtInput.text = ""
            return
        }

        if num! >= 0 && num! <= 100 {
            if check < 5 {
                arrayOfScore.append(num!)
                let maxcount = arrayOfScore.count

                if check == 0 {
                    scoreDisplay.text = String(arrayOfScore[maxcount-1])
                }
                else {
                    scoreDisplay.text! += ", " + String(arrayOfScore[maxcount-1])
                }
                check += 1
                txtInput.text = ""
            }

            let maxcount = arrayOfScore.count

            for i in (0...maxcount-1) {
                num = arrayOfScore[i]
                sum += num!
            }

            sum = sum / Double(maxcount)
            avgDisplay.text = String(sum)

            sum = 0
        }
    }

我必须声明var num才能使代码正常工作

1 个答案:

答案 0 :(得分:0)

代码中的几个问题已解决,请尝试是否有帮助

const str = "hell";

// this loop will set the offset. so if it's 1, "hell" will become "ellh"
for (let offset = 0; offset < str.length; offset++) {
  // this will contain the final string
  let output = "";

  // here we iterate through all the characters
  for (let index = 0; index < str.length; index++) {
    // and use modulo to switch 5 to 1 (in case length is 4)
    output += str[(index + offset) % str.length];
  }

  // there we go
  console.log(output);
}