Objective C 中 NSString 和 NSMutableString 对象的用法
我需要在整个程序中使用一堆字符串变量。我经常重新分配其中一些,而另一些则在执行过程中坚持使用相同的值。 这里的最佳实践是什么?
在第一种情况下,变量应该是 NSMutableString ,只要它们需要作为需要 的函数的参数,我就应该将它们强制转换为 NSString (使用复制方法) NSString 对象。是这样吗?
当我将它们重新分配给其他常量值时,我不应该处理以前的内容,对吧?
至于 NSString
对象,如果我需要为它们分配一个新值,我想我应该释放它们,再次分配它们,然后分配新值。这是正确的吗?
I need to use a bunch of string variables throughout my program. I reassign some of them quite often, while others are stuck with the same value during execution.
What's the best practice here?
In the first case, the variables should be NSMutableString
and I should cast them to NSString
(using the copy method) whenever they need to be arguments of functions that require NSString
objects. Is that right?
When I reassign them to other constant values, I shouldn't have to dispose the previous content, right?
As for NSString
objects, if I need to assign a new value to them, I guess I should deallocate them, allocate them again, and then assign the new value. Is that correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NSMutableString* 和 NSString* 之间的唯一区别是可变字符串可以更改。
您不必强制转换任何内容,因为 NSMutableString 是 NSString 的子类,也不必采取不同的内存措施(所以您是对的 * )。
如果您需要字符串的可修改版本,只需执行
NSMutableString* myMutableString = [NSMutableString stringWithString:myString];
你不应该“复制”任何东西。
请注意,如果您调用 :
myString 仍然是一个可变字符串。
因此,如果出于任何原因(安全......)你确实需要不可变的字符串,你必须调用
* 你是对的,但你也可以在可变字符串上调用 [replaceCharactersInRange:withString:] 。如果以前的分配有足够的空间,那么它可能会更快,因为没有破坏和新的分配要做。
(后来添加:忘记了setString:方法)
The only difference between NSMutableString* and NSString* is that mutable string can be changed.
You don't have to cast anything, since NSMutableString is a subclass of NSString, nor take different memory measures ( so you are right * ).
If you need a modifiable version of a string you just do
NSMutableString* myMutableString = [NSMutableString stringWithString:myString];
You should not 'copy' anything.
Note that if you call :
myString is still a mutable String.
So if for any reason (security...) you relly need unmutable strings, you have to call
* you are right, but you could also call [replaceCharactersInRange:withString:] on the mutable string. if there is enough space from previous allocation, then it may be faster, since there is no destruction and new allocation to do.
( Added later : forgot the setString: method )
除非您实际修改字符串,否则不应使用 NSMutableString。您要将整个字符串重新分配给新值,因此请继续使用常规
NSString
。使用自动释放版本,因为这始终比 alloc/init/release 更有效。如果您知道字符串将被分配给什么,您也可以将它们重新分配给常量。嗯,你可以这样做,但是效率非常。记住继承——
NSMutableString
是一个NSString
,只是添加了一些新的东西。一个简单的转换就可以解决问题:更好的是,您甚至不必这样做。由于继承,您可以在需要常规字符串的地方直接传入可变字符串,无需进行强制转换。这就是传承的魅力。
对既不可变也不可变的字符串。旧值会被简单地覆盖在内存中——没有什么可以处理的。就内存管理而言,一直创建新字符串确实效率不高。只需重新分配它们即可。您永远不需要多次
alloc
/init
一个字符串,并且单个init
应该由单个release<来平衡/代码>。
附录:什么时候应该使用 Mutable?
当您物理更改现有字符串的值而不完全丢弃旧值时,应使用可变字符串。示例可能包括在开头或结尾添加字符,或更改中间的字符。使用可变字符串,您可以“就地”执行此操作 - 只需修改现有字符串即可。相比之下,不可变字符串一旦设置了其值,就无法更改该值。
NSString
具有诸如stringByAppendingString:
之类的方法,它确实将字符串添加到现有字符串中,但它返回一个新字符串。在幕后,NSString
已将旧字符串复制到新的(更大)内存位置,添加参数并返回新字符串。这种复制的效率要低很多(相对而言,或者如果你必须做很多)。当然,没有什么可以阻止您将一个字符串物理分配给另一个字符串。旧值将被覆盖。大多数
NSString
,包括@"String Constants"
,都是自动释放的。如果您正在创建一个新字符串并决定分配/初始化,则可以将其分配给另一个值而不会产生任何后果:您可以使用可变和不可变字符串来执行此操作。主要的要点是,只有在更改字符串本身时才应该使用可变的。但是您可以使用可变和不可变字符串更改变量,而不会出现编译器或运行时问题(缺少内存管理,但其中大部分都是自动释放的)。
Unless you're actually modifying a string, you shouldn't use
NSMutableString
. You're reassigning the whole string to a new value, so stay with a regularNSString
. Use the autoreleased versions, because that'll be more efficient than alloc/init/release all the time. You could also just reassign your strings to constants if you know what they'll be assigned to.Well, you could do it that way, but it would be really inefficient. Remember inheritance—an
NSMutableString
is anNSString
, just with some new stuff tacked on. A simple cast will do the trick:Even better though, you don't even have to do that. Because of inheritance, you can directly pass in a mutable string wherever a regular string is required, no casting required. That's the beauty of inheritance.
For neither mutable or immutable strings. Old values are simply overwritten in memory—nothing to dispose of there. As far as the memory management goes, it's really not efficient to literally be creating new strings all the time. Just reassign them. You will never need to
alloc
/init
one string more than once, and that singleinit
should be balanced by a singlerelease
.Addendum: When Should You Use Mutable?
A mutable string should be used when you are physically changing the value of the existing string, without completely discarding the old value. Examples might include adding a character to the beginning or the end, or changing a character in the middle. With a mutable string, you can do this "in place"—you'll just modify the existing string. By contrast, an immutable string, once its value is set, cannot change that value.
NSString
has methods such asstringByAppendingString:
, which does add a string to an existing one—but it returns a new string. Behind the scenes,NSString
has copied your old string to a new (larger) memory location, added the argument, and returned the new string. That copying is a lot less efficient (relatively speaking, or if you have to do it a lot).Of course, there's nothing stopping you from physically assigning one string to another. Old values will be overwritten. Most
NSString
s, including the@"String Constants"
, are autoreleased. If you are creating a new string and you decide to alloc/init, you can then assign it to another value without consequence:You can do this with both mutable and immutable strings. The main takeaway is that you should only use mutable when your changing the string itself. But you can change the variable with both mutable and immutable strings without compiler or runtime issues (short of memory management, but most of it is autoreleased anyway).
如果您之前没有分配过 NSString,则不会释放它,如下所示:
您只需要在调用 alloc 时释放它:
You don't deallocate NSString if you didn't allocated it before, like here:
You only need to deallocate it when you call alloc: