ios - 使用 swift 在 XCode 中记分

标签 ios xcode swift label

所以我想在 XCode 项目中保留一个 Score 。但我希望它像 0.01, 0.02... 直到到达 1.00 并从 1 继续像 1, 2、3...

我当前的代码如下所示:

var Highscore = 0
var Score = 0

...

@IBAction func Action(sender: AnyObject) {

if (Score < 100) {

        Score++
        HighScoreLabel.text = "HighScore:\(Score)"

} else if (Score >= 100) {

        Score += 100
        HighScoreLabel.text = "HighScore:\(Score)"

我已经尝试创建一个除分数的变量,我尝试将其设为 double (但我想要 1, 2.. 而不是 1.00, 2.00),我不知道该怎么办。 如果可以的话请帮助我,并随时进行编辑。

最佳答案

func action() {
    if (score < 100) {
        score += 1
        let someScore = score < 1 ? String(format: "%.2f", score) : String(format: "%.f", round(score))
        println("HighScore:\(someScore)")
    } else if (score >= 100) {
        score += 100
        let someScore = score < 1 ? String(format: "%.2f", score) : String(format: "%.0f", round(score))
        println("HighScore:\(someScore)")
    }
}

这应该可以。

编辑:
使用了您的代码。

关于ios - 使用 swift 在 XCode 中记分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30282611/

相关文章:

iphone - 试图弄清楚这是否是内存泄漏 : Xcode

ios - 图像 Assets 目录和 iOS 设备系列

ios - UITableView 内部的 UIScrollView 搞乱了

ios - 进程启动失败 : failed to get the task for process 2847

iOS 6 : How do I restrict some views to portrait and allow others to rotate?

c++ - cocos2d 中的 setScale 特定时间 Xcode

ios - 快速相机覆盖 View

ios - 在 Swift 中将 12 小时 UTC 日期转换为 24 小时本地时区格式

swift - collisionmode 可以设置为空吗?

arrays - 如何使用变量将字符串追加到数组中? swift