Why Swift Array cannot be changed in didSet property observer?

Seems like Swift Array won't go through didSet, why?

var intArray: [Int] = [] {
    didSet {
        intArray += [0]
    }
}

if intArray.count == 0 {
    println("Why is intArray not being altered?")
}

      

+3


source to share


1 answer


willSet

and didSet

are not called when the variable is first initialized, so normal behavior and valid for all property types does not matter.

Try this in the playground:

var intArray: [Int] = [] {
    didSet {
        intArray += [0]
    }
}

intArray = []

intArray

      



the last statement shows that intArray

- [0].

Read the second post on Property Observers

+4


source







All Articles