0

There is a computed property computedArray and a stored property storedArray in my programme, both of which get values from DataController.shared.updateArray(withCurrentTimestamp: DataController.currentTimestamp)
The value of currentTimestamp changes over time, but when I print out DataController.shared.computedArray and DataController.storedArray, the result of DataController.shared.computedArray changes with currentTimestamp, while DataController.storedArray never changes. what's the reason of it?

struct DataController {

    static let shared = DataController()

    var computedArray: [TimeInterval] {
        get {
            DataController.shared.updateArray(withCurrentTimestamp: DataController.currentTimestamp)
        }
    }
    
    static var storedArray = DataController.shared.updateArray(withCurrentTimestamp: DataController.currentTimestamp)

}
zeytin
  • 5,545
  • 4
  • 14
  • 38
P.H.
  • 15
  • 5
  • 1
    `storedArray ` is computed only one time, while `computedArray` is computed each time you access it – olha Nov 19 '20 at 07:49
  • Another thing to note is that arrays are value types, which means every time you update an array, it creates and returns a new array. That means that although you’re getting the shared array in the computed variable, you’re still returning a new array. The shared array remains unchanged. – kbunarjo Nov 19 '20 at 08:14

0 Answers0