let/var如何解决可变性? [英] How does let/var resolve mutability?

查看:95
本文介绍了let/var如何解决可变性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有任何问题,我想澄清一下关于可变性的问题.

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

在Objective-C中,我们将使用 NSMutableArray 来获取可变数组,使用 NSArray 来获取不可变数组.我对两者的内部运作了解不多,但是从我的记忆中我相信,不同之处在于 NSArray 仅保留特定于初始值的内存量,这使它更效率很高,而 NSMutableArray 不知道它将需要多少内存.大概这意味着 NSMutableArray 拥有指向存储位的指针,这些指针到处都是,而不像 NSArray 那样一一对应吗?还是只是保留大量内存,希望它不会用完?

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?

在Swift中,显而易见的替代方法是 let 表示不可变,而 var 表示可变.如果使用这些关键字声明变量,那么我认为Swift和Objective-C之间没有区别.但是,如果我声明不带var/let的变量(例如将其存储在另一个变量中),我将无法理解它的工作原理.

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.

假设我有一本字典,例如 [String:[String]] .换句话说,对于每个字符串键,都有一个字符串数组.考虑以下情况:

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")

这项工作可以吗?

我想关键的问题是,在Objective-C中,我总是定义我要使用 NSMutableArray 还是 NSArray .使用文字语法 [@"string"] 创建数组始终会导致 NSArray ,除非我指定它,否则它将是可变的.在Swift中,我不知道什么时候可变.

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.

谢谢

推荐答案

对于数组和字典, let var 关键字决定整个集合是可变的还是可变的一成不变的.换句话说,如果使用 let 关键字声明字典是不可变的,则无法更改其任何值,因此第二个示例将不起作用.

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.

Swift 中,确定集合是可变的还是不可变的仅取决于用于声明它的关键字,因此使用 let 关键字声明数组/字典将等同于声明一个不可变的数组(在Objective-C中为 NSArray ),同时使用 var 关键字进行声明将为您提供一个可变的数组( NSMutableArray 在Objective-C中).

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).

这篇关于let/var如何解决可变性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆