减少UserDefaults整数值 - Swift

时间:2017-04-11 13:50:51

标签: swift userdefaults

有没有办法减少UserDefaults整数键值?

这是我的保存代码:

    var highScore = UserDefaults().integer(forKey: "HIGHSCORE")

如何将此值减少一个固定的整数?

那么减少20等?

2 个答案:

答案 0 :(得分:3)

首先,获取高分的旧值。然后,在最终保存新值之前进行算术运算:

double       value;
datetime     time;                      // - your values from file are here

int          shift  = iBarShift( _Symbol, 0, time );
ExtBufferCOT[shift] = value;            /* probably need to fill buffer
                                           of next candles too
                                           if your chart timeframe
                                           is smaller then frequency
                                           of observations in the file
                                           */

答案 1 :(得分:0)

var highScore: Int {
  get {
     return Userdefaults.standard.integer(forKey: “highScore”)
   } set {
Userdefaults.standard.set(newValue forKey: “highScore”)
   }
}

可以通过

设置高分
highScore = 5