Swift是“可变的"吗?字符串真的可变,还是像Java字符串一样? [英] Are Swift "mutable" strings really mutable, or are they just like Java strings?

查看:99
本文介绍了Swift是“可变的"吗?字符串真的可变,还是像Java字符串一样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Swift编程语言中,在字符串"部分的"字符串可变性" 小节中,它表示:

In The Swift Programming Language, in the section on Strings, subsection String Mutability, it says this:

您可以通过将特定的String分配给变量(在这种情况下可以对其进行修改)或常量(在这种情况下可以对其进行修改)来指示是否可以对其进行修改(或 mutant ).无法修改):

You indicate whether a particular String can be modified (or mutated) by assigning it to a variable (in which case it can be modified), or to a constant (in which case it cannot be modified):

并给出示例代码:

var variableString = "Horse"
variableString += " and carriage"
// variableString is now "Horse and carriage"

let constantString = "Highlander"
constantString += " and another Highlander"
// this reports a compile-time error - a constant string cannot be modified"

在iBooks 此处或在网络浏览器

The book in iBooks here, or in a web browser here.

在下一段中,它声称字符串是值类型".

In the next paragraph it claims that "strings are value types".

我的问题:对我来说,这看起来像是个可变的字符串.看起来就像我在Java(或C#,Python和其他语言)中所惯用的:具有可变变量绑定的不可变字符串对象.换句话说,有一个对象马",然后它创建了一个新的字符串对象马和马车",并将其设置为相同的变量.而且由于无法分辨不可变对象的引用和值类型之间的区别(对吗?),我想知道:为什么他们这样描述它?这些Swift字符串与其在Java中的方式之间有什么区别吗? (或C#,Python,Objective-C/NSString)

My question: that doesn't look like a mutable string to me. It looks like what I'm used to in Java (or C#, Python, and others): immutable string objects with mutable variable bindings. In other words, there was an object "Horse" and then it created a new String object "Horse and carriage" and set it to the same variable. And since there is no way to tell the difference between an reference to an immutable object versus a value type (right?), I wonder: why are they describing it like this? Is there any difference between these Swift strings and the way it is in Java? (Or C#, Python, Objective-C/NSString)

推荐答案

在某种程度上,可变"和不可变"仅在谈论引用类型时才有意义.如果您尝试将其扩展为值类型,则所有值类型在功能上都等效于不可变"引用类型.

In a certain way, "mutable" and "immutable" only make sense when talking about reference types. If you try to extend it to value types, then all value types can be considered functionally equivalent to "immutable" reference types.

例如,考虑类型为Intvar.这是可变的吗?你们中的某些人可能会说肯定的-您可以通过为其分配(=)来更改其可见的值".但是,对于NSNumberNSStringvar来说,也可以这么说-您可以通过为其分配可见值来对其进行更改.但是NSNumberNSString被描述为不可变类.

For example, consider a var of type Int. Is this mutable? Some of you might say, sure -- you can change its visible "value" by assigning (=) to it. However, the same can be said of a var of NSNumber and NSString -- you can change its visible value by assigning to it. But NSNumber and NSString are described as immutable classes.

引用类型真正发生的事情是,给它们赋值会导致变量(指针)指向一个新对象.旧对象或新对象本身都不会更改",但是由于它指向另一个对象,因此您可以看到"一个新值.

What is really happening for reference types is that assigning to them causes the variable (a pointer) to point to a new object. Neither the old nor new object itself is "changed", but since it points to a different object, you "see" a new value.

当我们说一个类是可变的"时,我们的意思是它提供了一个API(方法或引用)来实际改变对象的内容.但是,我们如何知道对象已更改? (而不是它是一个新对象?)这是因为我们可以对同一个对象进行另一个引用,并且通过一个引用对对象的更改可以通过另一个引用看到.但是这些属性(指向不同的对象,具有指向同一对象的多个指针)本质上仅适用于引用类型.根据定义,值类型不能具有这种共享"(除非值"的一部分是引用类型,如Array中所述),因此,值类型不会发生可变性"的结果.

What we mean when we say a class is "mutable" is that it offers an API (method or reference) to actually change the contents of the object. But how do we know that the object has changed? (rather it being a new object?) It's because we could have another reference to the same object, and changes to the object through one reference is visible through another reference. But these properties (pointing to different objects, having multiple pointers to the same object) inherently only apply to reference types. Value types, by definition, cannot have such "sharing" (unless part of the "value" is a reference type, like in Array), and thus, the consequence of "mutability" cannot happen for value types.

因此,如果您制作了一个包装整数的不可变类,则它在操作上等效于Int -在两种情况下,更改变量值的唯一方法是为其分配(=) .因此,Int也应类似地视为不可变的".

So if you make an immutable class that wraps an integer, it would be operationally equivalent to an Int -- in both cases, the only way to change a variable's value would be to assign (=) to it. So Int should also similarly be considered "immutable".

Swift中的值类型稍微复杂一些,因为它们可以具有方法,其中一些可以是mutating.因此,如果可以在值类型上调用mutating方法,它是否可变?但是,如果我们考虑在值类型上调用mutating方法作为语法糖来为其分配一个全新的值(无论该方法将其突变为什么),我们都可以克服.

Value types in Swift are slightly more complex, because they can have methods, some of which can be mutating. So if you can call a mutating method on a value type, is it mutable? However, we can overcome this if we consider calling a mutating method on a value type to be syntactic sugar for assigning a whole new value to it (whatever the method would mutate it to).

这篇关于Swift是“可变的"吗?字符串真的可变,还是像Java字符串一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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