gpt4 book ai didi

ios - 将分数添加到 NSUserDefault

转载 作者:行者123 更新时间:2023-11-29 11:56:48 25 4
gpt4 key购买 nike

A 有一些逻辑,但它没有像我想要的那样工作。一切都很好,直到 coinNumber 变为 0 然后它重新启动到 100 (var coins = 100)

目标是从 100 开始。然后当你玩它时它有 - 或 + 硬币并且它保存到另一个变量。

public var coins = 100 // at start got 100 coins


var coinNumber = NSUserDefaults.standardUserDefaults().integerForKey("coins")

func addCoins400() { // adding 400 coins and save it
coins = coins + 400

let CoinsDefault = NSUserDefaults.standardUserDefaults()
CoinsDefault.setInteger(coins, forKey: "coins")

}

if coinNumber != 0 {
coins = coinNumber
}

@IBAction func showAnswer(sender: UIButton) {

if coins >= 50 {
// I show answer
coins = coins - 50


let CoinsDefault = NSUserDefaults.standardUserDefaults()
CoinsDefault.setInteger(coins, forKey: "coins")
CoinsDefault.synchronize()

最佳答案

尝试同步您的NSUserDefaults

  public var coins = 100 // at start got 100 coins



var coinNumber = NSUserDefaults.standardUserDefaults().integerForKey("coins")

func addCoins400() { // adding 400 coins and save it
coins = coins + 400

let CoinsDefault = NSUserDefaults.standardUserDefaults()
CoinsDefault.setInteger(coins, forKey: "coins")
CoinsDefault.synchronize()
}

if coinNumber != 0 {
coins = coinNumber
}

关于ios - 将分数添加到 NSUserDefault,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38775021/

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