有一些逻辑,但它不像我想要的那样工作。一切都很好,直到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()
答案 0 :(得分:1)
尝试 synchronizing
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
}