I was recently working on some test code, building silly view hierarchys and I ran across this little bit of code that made me squint a little extra hard.
var parentView: UIView = UIView() //Warning here
parentView.addSubview(UIImageView())
On the first line is the following warning:
Variable 'parentView' was never mutated; consider changing to 'let' constant
I'm confused as to why this element is not, or why the compiler believes it is not, being mutated. I thought perhaps that Swift was detecting that a collection within the object was being mutated, and not the properties of the object itself. This lead me to attempt the following:
let strings: [String] = []
strings.append("Hi") //This is an error
Given the above code, the only explanation I can come up with is that there is a bug somewhere. Am I missing a keyword somewhere that allows this situation to make sense? Something along the line's of C++'s "mutable" keyword?
If it's not a bug, I'd like to see this scenario re-created in a class that does not inherit from UIObject, so I can understand what causes this.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…