NSStrings 的最终结论:可变和不可变

发布于 2024-07-21 07:55:35 字数 389 浏览 15 评论 0原文

我读过几本书……以及在线……有关不可变和可变字符串的内容。 他们声称“不可变字符串”无法更改。 (但他们从未定义“更改”。)

哪些 NSString 可以在不使用 NSMutableString 的情况下更改?

该字符串包含“catfish”......后来我尝试将其更改为“cat”。 (相同的字母,只是更短。)

它包含“猫”......我尝试将其更改为“鲶鱼”。 (类似的开始......但只是更长。)

我将“cat”更改为“CAT”。 (相同的字母,但只是大小写改变了。)

我将“cat”更改为“dog”。 (完全不同的字符串,但长度相同。)

我将“cat”更改为“dogwood”。 (完全不同的字符串,但更长。)

I've read in several books... and online... about immutable and mutable strings.
They claim "immutable strings" can't be changed.
(But they never define "change".)

Which of these NSStrings could be changed without using NSMutableString?

The string contains "catfish"... and I later try to change it to "cat".
(Same letters, just shorter.)

It contains "cat"... and I try to change it to "catfish".
(Similar start... but just made longer.)

I change "cat" into "CAT".
(Same letters, but just the case has changed.)

I change "cat" into "dog".
(Totally different string, but the same length.)

I change "cat" into "dogwood".
(Totally different string, but longer.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

你的往事 2024-07-28 07:55:35

答案是——没有。 不可变意味着它不能更改为与原始字符串不同的任何。 每个字符保持不变,长度不能更改等。一旦定义,您就无法更改该字符串的任何内容。 如果你想延长或缩短它或更改任何字符,你需要创建一个新字符串(或使用 NSMutableString 开始)。

The answer is - none. Immutable means it cannot be changed into anything different than the original string. Every character stays the same, the length cannot change, etc. Once it's defined you cannot change anything about that string. If you want to make it longer or shorter or change any of the characters, you need to make a new string (or use a NSMutableString to begin with).

微凉 2024-07-28 07:55:35

如果我声明一个变量:

NSString * var;
// Here some code giving a new value to var

不可变的是 var 指向的对象,而不是 var 本身。

这意味着我无法更改 var 所指向的对象的任何内容,但我可以更改所指向的对象。

var 上不允许您提到的任何操作,但您可以使用另一个不同的字符串分配 var

NSString * anotherVar;
// Here some code giving a new value to anotherVar

var = anotherVar; // This is allowed (with a memory leak if GC is disabled)

// The following is also allowed: -stringWithFormat: creates a new object
var = [var stringWithFormat:@"%@ with more chars", var];

If I declare a variable:

NSString * var;
// Here some code giving a new value to var

What is immutable is the object pointed to by var, not var itself.

This means that I cannot change anything of the object pointed to by var, but I can change which object is pointed to.

None of the operations you mention is allowed on var, but you can assign var with another different string:

NSString * anotherVar;
// Here some code giving a new value to anotherVar

var = anotherVar; // This is allowed (with a memory leak if GC is disabled)

// The following is also allowed: -stringWithFormat: creates a new object
var = [var stringWithFormat:@"%@ with more chars", var];
巴黎夜雨 2024-07-28 07:55:35

NSString 无法编辑,每次将字符串值分配给 NSString 对象时,它都会孤立旧的字符串对象并重新创建新的字符串对象。 如果您正在处理大型字符串缓冲区,那么这是一项昂贵的操作。 如果您正在处理大型字符串缓冲区,请使用 NSMutableString 以获得最佳性能。 这类似于 .net 中的 string 与 stringBuilder。

NSString cannot be edited, every time you assign a string value to NSString object it will orphan the old string object and re-create the new string object. That is an expensive operation if you are dealing with large string buffer. If you are dealing with large string buffer then use NSMutableString for optimum performance. This is similar to string vs stringBuilder in .net.

橘虞初梦 2024-07-28 07:55:35

你也需要定义“change”:)

基本上,当你创建一个 NSString 时,你正在创建一个字符数组,并且你告诉编译器该数组的内容永远不会改变。 “永不改变”的部分就是“不可变”的部分; 字符串包含的数据永远无法修改。

另一方面, NSMutableString 允许您实际更改它指向的数据(因此它是“可变的”)。 注意 NSMutableString 存在的 'appendString' 方法; 这实际上获取数据并将其放在字符串的末尾。

例如,如果您有:

NSString *myString = @"Cat";
myString = @"金鱼";

那会很好用。 您实际上从未更改 myString 包含的数据; 你只是告诉它你希望它指向一个新的不可更改的数据段。

唯一一次真正遇到可变对象和不可变对象之间差异的问题是,如果您尝试直接修改 myString(例如,向其附加“s are Cool”或其他内容)。

换句话说,这两个类都允许您将任何字符串“更改”为任何其他字符串,但是您用来执行此操作的方法将非常不同。 例如,要将可变字符串的“cat”转换为“CAT”,您可以迭代每个字符并简单地将其变为大写(因为您可以修改可变字符串的内容)。 另一方面,对于不可变字符串,您必须首先将原始字符串复制到其他地方,修改它,然后重定向变量以指向这个新的 NSString。

以下链接可能有用:
http://www.kirupa.com/forum/showthread.php?t= 307928
http://forums.macrumors.com/showthread.php?t=467099

我还建议您进行一些谷歌搜索; 这是一个关系到发展全局的问题,是一个比较重要的概念。 互联网上其他地方也对此进行了热烈讨论。

You need to define "change", too :)

Basically, when you create an NSString, you're creating an array of characters and you're telling the compiler that the contents of said array will never change. The "never change" part is the "immutable" part; the data the string contains can't ever be modified.

On the other hand, an NSMutableString allows you to actually change the data it points to (hence it's 'mutable'). Note the 'appendString' method that exists for NSMutableString; this actually takes data and slaps it onto the end of the string.

For example, if you had:

NSString *myString = @"Cat";
myString = @"Goldfish";

That would work fine. You're never actually changing the data myString contains; you're simply telling it that you want it to point to a new segment of unchangeable data.

The only time you'd actually run into problems with the differences between mutable and immutable objects is if you tried to actually modify myString directly (e.g. append "s are cool" to it or something).

In other words, either class would allow you to "change" any string into any other string, but the methods you used to do it would be very different. For example, to turn "cat" into "CAT" with a mutable string, you could iterate over every character and simply make it uppercase (since you can modify the contents of a mutable string). With an immutable string, on the other hand, you'd have to first copy the original string elsewhere, modify it, and then redirect your variable to point to this new NSString.

The following links might be useful:
http://www.kirupa.com/forum/showthread.php?t=307928
http://forums.macrumors.com/showthread.php?t=467099

I'd also recommend doing some googling; this is a question that relates to development in general, and it's a relatively important concept to grasp. It's also been discussed to death elsewhere on the internet.

手心的温暖 2024-07-28 07:55:35

不可变字符串(或者实际上任何不可变对象)是在初始化后根本无法更改的字符串。

这是在(伪)伪代码中使用不可变字符串的优点:

// if using mutable strings
let a = "Hello World";
let b = a;
a.replace("Hello", "Goodbye");

现在 a 持有什么? “你好世界”? “世界再见”?
现在 b 持有什么?

如果字符串是可变的,那么它可能保存“再见世界”。 这可能就是你想要的。 可能不是。

有了不变性,很明显 b 仍然指向原始字符串,因为替换方法不能更改实际字符串,而是返回对新字符串的引用。

在此示例中您可能看不到太多好处。 但是,如果 a 和 b 在代码的不同部分或不同线程中引用和使用,则具有不可变引用可以消除许多潜在错误。

An immutable string (or indeed, any immutable object) is one that can't be changed at all after initialisation.

Here's the advantage of using immutable strings, in (pseudo-)pseudo-code:

// if using mutable strings
let a = "Hello World";
let b = a;
a.replace("Hello", "Goodbye");

Now what does a hold? "Hello World"? "Goodbye World"?
Now what does b hold?

If strings are mutable, then it might hold "Goodbye World". This may be what you want. It might not be.

With immutability, it becomes very clear that b still points to the original string, because the replace method can't have changed the actual string, but rather returned a reference to a new string.

You might not see much benefit in this example. But if a and b are referenced and used in different parts of the code, or in different threads, having immutable references removes a lot of potential errors.

罗罗贝儿 2024-07-28 07:55:35

默认情况下,Objective C 语言中使用的 NSString 是不可变的。 NSMutableString 用于声明 & 定义一个可变字符串。 不可变字符串中的任何内容都无法更改。 不是长度,不是字符,什么都不是。

By default, NSString used in objective C language is immutable. NSMutableString is used to declare & define a mutable string. Nothing in an immutable string can be changed. Not the length, not the characters, nothing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文