如何更新类初始化程序?

时间:2015-04-10 11:26:40

标签: ios swift

我有类使用类初始值设定项的ProgressBarLabelValues。

class ProgressBarLabelValues {

    var stepsLeft: Double
    var stepsRight: Double
    var weightLeft: Double
    var weightRight: Double
    var distanceLeft: Double
    var distanceRight: Double


    init(stepsLeft: Double, stepsRight: Double, weightLeft:Double, weightRight: Double, distanceLeft:Double, distanceRight: Double){
        self.stepsLeft = stepsLeft
        self.stepsRight = stepsRight
        self.weightLeft = weightLeft
        self.weightRight = weightRight
        self.distanceLeft = distanceLeft
        self.distanceRight = distanceRight
    }
}

var progressBarLabelValues = ProgressBarLabelValues(
    stepsLeft: 0.0,
    stepsRight: goal.steps,
    weightLeft: goal.weight-5,
    weightRight: goal.weight,
    distanceLeft: 0,
    distanceRight: goal.distance)

无论如何,目标是在目标类中设定的:

class Goal {

    var steps:Double
    var weight:Double
    var distance:Double

    init(steps:Double, weight: Double, distance: Double){
        self.steps = steps
        self.weight = weight
        self.distance = distance
    }
}


var goal = Goal(
    steps: NSUserDefaults.standardUserDefaults().doubleForKey("GoalSteps"),
    weight:NSUserDefaults.standardUserDefaults().doubleForKey("GoalWeight"),
    distance: NSUserDefaults.standardUserDefaults().doubleForKey("GoalDistance")    
)

我的问题是,当我更新步骤NSUserDefaults.standardUserDefaults().setDouble(10000, forKey: "GoalSteps")

的目标时

progressBarLabelValues.stepsRight不会在ViewController中更新。 它没有注意到goal.steps已被更改。如果变量目标是一个函数,我可以要求它通过代码目标()进行更新。是否有可能要求变量目标(类初始化程序)像函数一样更新? 任何帮助将非常感谢!谢谢。

1 个答案:

答案 0 :(得分:1)

  • progressBarLabelValues是变量
  • 这不是一个元组,它只是一个普通的类初始化器
ProgressBarLabelValues(
    stepsLeft: 0.0,
    stepsRight: goal.steps,
    weightLeft: goal.weight-5,
    weightRight: goal.weight,
    distanceLeft: 0,
    distanceRight: goal.distance)

您似乎忘记致电NSUserDefaults.standardUserDefaults().synchronize()来保存更改:

let defaults = NSUserDefaults.standardUserDefaults()
defaults.setDouble(10000, forKey: "GoalSteps")
defaults.synchronize()

编辑:您遇到的问题是,您只能在创建goal对象时从NSUserDefaults读取。当您需要更改其值时,只需访问该属性:

goal.steps = 1000

这将更改内存中模型对象的内容。我强烈建议您在Properties

上阅读Apple的Swift一书

编辑2:解决问题的最佳方法是每次访问setter时,都会保存到NSUserDefaults。也许这对磁盘来说太过分了,你需要考虑一下。您可以在游乐场尝试的完整列表:

import Foundation

class ProgressBarLabelValues {

    var stepsLeft: Double
    var stepsRight: Double
    var weightLeft: Double
    var weightRight: Double
    var distanceLeft: Double
    var distanceRight: Double


    init(stepsLeft: Double, stepsRight: Double, weightLeft:Double, weightRight: Double, distanceLeft:Double, distanceRight: Double){
        self.stepsLeft = stepsLeft
        self.stepsRight = stepsRight
        self.weightLeft = weightLeft
        self.weightRight = weightRight
        self.distanceLeft = distanceLeft
        self.distanceRight = distanceRight
    }
}
class Goal {
    var _steps: Double = 0
    var steps:Double {
        get {
            return self._steps
        }
        set {
            NSUserDefaults.standardUserDefaults().setDouble(newValue, forKey: "GoalSteps")
            self._steps = newValue
        }
    }
    var weight:Double = 10
    var distance:Double = 10

    init(steps:Double, weight: Double, distance: Double){
        self.steps = steps
        self.weight = weight
        self.distance = distance
    }
}


var goal = Goal(
    steps: NSUserDefaults.standardUserDefaults().doubleForKey("GoalSteps"),
    weight:NSUserDefaults.standardUserDefaults().doubleForKey("GoalWeight"),
    distance: NSUserDefaults.standardUserDefaults().doubleForKey("GoalDistance")
)

var progressBarLabelValues = ProgressBarLabelValues(
    stepsLeft: 0.0,
    stepsRight: goal.steps,
    weightLeft: goal.weight-5,
    weightRight: goal.weight,
    distanceLeft: 0,
    distanceRight: goal.distance)



goal.steps = 1000