gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:28:31 26 4
gpt4 key购买 nike

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....

这是 View Controller 中的代码,不多。非常简单的秒表应用程序。

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)
}


}

我尝试过设置约束,但这只会到处发出“模棱两可”的警告。有什么建议么?有想法吗?

最佳答案

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

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

关于ios - 使用 += 表达式初始化字符串返回 "()",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43882027/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com