DidSet on properties of type Array

IIRC, didSet on properties of type Array was called if I change the array with append()


It seems that Swift in Xcode7.3 Beta 2 doesn't do this any longer.


import Foundation
class Container {
    var strings: [String] = [] {
        didSet {
            Swift.print("Change strings: \(self.strings)")
        }
    }
}
let container = Container()
container.strings = ["One"]
container.strings.append("Two")


Is this just a bug or intended?

I'm using Beta 2. It prints to the console, but if you want to see it in a playground on the right, you have to take away the "Swift.".

With 7.3 Beta 1 I get following output:


Change strings: ["One"]

Change strings: ["One", "Two"]


With 7.3. Beta 2:


Change strings: ["One"]

Right you are. I dont remember what I did to get the behavior I mentioned.


Make the class final and you're good to go!


If you cannot make the class final, log a bug with Swift. Non-final classes should be considered bugs at this point.

I've noticed the same problem. However didSet for arrays have been always confused me, because didSet sounds like the variable reference is changed. So maybe Apple engineers decided to get rid of it? I've been looking for any information in release notes. Nothing found.

I have not installed 7.3 Beta2, so I can't test.


What happens if changing container.strings.append("Two") by container.strings = ["One","Two"] ?


Do you get a single print or are the 2 print the same Change strings:"One"] ?

Great, at least I have a workaround with "final".

I don't have any information either, but because if you use final classes didSet works again, I think it is just a bug.

You're right. I read the post about final class and thought it's a misunderstanding of the topic. Making the class final does the work for now. Let's wait for next beta. However a voice from an Apple engineer would be great.

I'm under the impression that because the Swift team doesn't believe in non-final classes, this weird stuff will happen if the classes are not final, more often, because subclassable things are not put through their paces as much. This may not be true, at all, but I kind of hope it is because I despise inheritance but love protocol extensions and if Apple isn't doing a good job maintaining inheritance, people will stop using it even though they think they want to use it.


I only discovered this weird workaround due to my brain seeing a lack of the final keyword and subsequently forcing it into existence through my fingers. I hoped that because I only like final classes, it would be a solution, magically. That that actually happened was quite a surprise to me. 😁

DidSet on properties of type Array
 
 
Q