NSStrings的最后一个单词:Mutable和Immutable [英] The final word on NSStrings: Mutable and Immutable

查看:151
本文介绍了NSStrings的最后一个单词:Mutable和Immutable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了几本书...和在线...关于不可变和可变字符串。
他们声称不可变字符串不能更改。

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

在不使用NSMutableString的情况下,哪些NSStrings可以被修改?

Which of these NSStrings could be changed without using NSMutableString?

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

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

它包含cat...并且我尝试将其更改为catfish。
(类似的开始...但只是变得更长了。)

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

我将cat改成CAT。
(相同的字母,但是情况已经改变了。)

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

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

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

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

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

推荐答案

不可变意味着它不能更改为与原始字符串不同的任何。每个字符保持不变,长度不能改变,等等。一旦定义,你不能改变任何关于该字符串。如果你想让它更长或更短或更改任何字符,你需要一个新的字符串(或使用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).

这篇关于NSStrings的最后一个单词:Mutable和Immutable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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