How does let/var resolve mutability? [duplicate]

末鹿安然 提交于 2021-01-28 02:50:29

问题


I don't have any problem, i would just like some clarification on an issue regarding mutability.

In Objective-C we would use for example NSMutableArray to get a mutable array and an NSArray to get an immutable one. I don't know much about the inner workings of the two, but from what I can remember I believe that the difference is that NSArray only reserves an amount of memory specific to the initial value which makes it more efficient, while NSMutableArray has no idea how much memory it will require. Presumably this means that NSMutableArray has pointers to bits of memory that are all over the place and not one by one like with NSArray? Or does it perhaps just reserve a lot of memory hoping it won't run out?

In Swift the obvious substitution is let for immutable and var for mutable. If a variable is declared with these keywords that I see no difference between Swift and Objective-C. However, I don't understand how it works if I declare the variable without the var/let by, for example, storing it in another variable.

Let's say I have a dictionary such as [String: [String]]. In other words, for each string key there is an array of strings. Consider the following case:

var dictionary: [String: [String]] = [:]
dictionary["key"] = ["string0", "string1"]

//dictionary now is ["key": ["string0", "string1"]]

But what is the strings array now? Is it mutable because the dictionary is mutable? Is it mutable because everything we assign is mutable? How about the following case:

let dictionary = ["key": ["string0", "string1"]]
dictionary["key"].append("string2")

Would this work?

I guess the key issue is that in Objective-C I always define whether am I working with NSMutableArray or NSArray. Creating an array using the literal syntax [@"string"] always leads to an NSArray and it won't be mutable unless I specify it. In Swift I don't know when is what mutable.

Thanks


回答1:


For arrays and dictionaries, the let or var keyword decides whether the whole collection would be mutable or immutable. In other words, if you declare a dictionary immutable by using the let keyword, you cannot change any of its values, so the second example would not work.

In Swift deciding whether a collection will be mutable or immutable only depends on the keyword you use to declare it, so declaring an array/dictionary using the let keyword will be equivalent to declaring an immutable array (NSArray in Objective-C) while declaring it with the var keyword will give you a mutable array (NSMutableArray in Objective-C).




回答2:


If you create an array, a set, or a dictionary, and assign it to a variable, the collection that is created will be mutable. This means that you can change (or mutate) the collection after it is created by adding, removing, or changing items in the collection. If you assign an array, a set, or a dictionary to a constant, that collection is immutable, and its size and contents cannot be changed.

So, let means constant, if you declare array or dictionary using let it will be immutable.

if you declare array or dictionary as var it will be mutable.

So, the case below will not work because dictionary will be immutable:

let dictionary = ["key": ["string0", "string1"]]
dictionary["key"].append("string2")

Check the reference here:

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html



来源:https://stackoverflow.com/questions/45586905/how-does-let-var-resolve-mutability

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!