[swift-evolution] constant var
Magnus Ahltorp
map at kth.se
Thu Dec 14 03:38:09 CST 2017
> 14 Dec. 2017 17:52 Inder Kumar Rathore . via swift-evolution <swift-evolution at swift.org> wrote:
>
> class MyClass {
> private var myDict = [String : String]()
>
> func addMemebr() {
> self.myDict["key"] = "value" // Ok for me
> }
>
> func anotherFunc() {
> self.myDict = [String : String]() // Not okay for me, I don't want any code to do this within the class
> }
> }
But in Swift, since myDict is a Dictionary, it is *one* value, so it doesn't make sense to allow one but not the other. Also, since it is one value, you will not be able to detect the difference between setting a part of a dictionary and the whole dictionary. If Dictionary had been a class, there would be a non-changing reference, but there isn't in this case. Conceptually, the whole dictionary is copied every time you set it or assign it.
/Magnus
More information about the swift-evolution
mailing list