用+ =表达式初始化字符串返回"()"

时间:2017-05-10 01:04:10

标签: ios swift xcode

Start of app (Main storyboard but it's fine in simulator to.)

After clicking play button in mutates into something that isn't a 1,2,3....

以下是viewcontroller中的代码,但并不多。非常简单的秒表应用程序。

import UIKit

class ViewController: UIViewController {


    @IBOutlet weak var timeLabel: UILabel!
    var counter = 0
    var clock : Timer = Timer()

    override func viewDidLoad() {
        timeLabel.text = String(counter)
        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 playButton(_ sender: Any) {
        clock = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
    }

    func updateTimer(){
        timeLabel.text = String(describing: counter += 1)
    }

    @IBAction func pauseButton(_ sender: Any) {
        clock.invalidate()
    }

    @IBAction func resetButton(_ sender: Any) {
        clock.invalidate()
        counter = 0
        timeLabel.text = String(counter)
    }


}

我已经尝试过设置约束条件,但这只是一种模棱两可的问题。警告到处都是。有什么建议?想法?

1 个答案:

答案 0 :(得分:0)

问题在于您的updateTimer方法。您需要将变量增量移动到上面的行,然后从变量中创建字符串表示:

func updateTimer() {
    counter += 1
    timeLabel.text = String(counter)
}