Objective C 中 NSString 和 NSMutableString 对象的用法

发布于 2024-11-18 10:39:45 字数 314 浏览 3 评论 0原文

我需要在整个程序中使用一堆字符串变量。我经常重新分配其中一些,而另一些则在执行过程中坚持使用相同的值。 这里的最佳实践是什么?

在第一种情况下,变量应该是 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 技术交流群。

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

发布评论

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

评论(3

微凉徒眸意 2024-11-25 10:39:46

NSMutableString* 和 NSString* 之间的唯一区别是可变字符串可以更改。
您不必强制转换任何内容,因为 NSMutableString 是 NSString 的子类,也不必采取不同的内存措施(所以您是对的 * )。

如果您需要字符串的可修改版本,只需执行

NSMutableString* myMutableString = [NSMutableString stringWithString:myString];

你不应该“复制”任何东西。

请注意,如果您调用 :

NSString* myString = myMutableString;

myString 仍然是一个可变字符串。

因此,如果出于任何原因(安全......)你确实需要不可变的字符串,你必须调用

NSString* myString = [NSString stringWithString:myMutableString];

* 你是对的,但你也可以在可变字符串上调用 [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 :

NSString* myString = myMutableString;

myString is still a mutable String.

So if for any reason (security...) you relly need unmutable strings, you have to call

NSString* myString = [NSString stringWithString:myMutableString];

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

巷子口的你 2024-11-25 10:39:45

除非您实际修改字符串,否则不应使用 NSMutableString。您要将整个字符串重新分配给新值,因此请继续使用常规 NSString。使用自动释放版本,因为这始终比 alloc/init/release 更有效。如果您知道字符串将被分配给什么,您也可以将它们重新分配给常量。

在第一种情况下,变量应该是 NSMutableString,只要它们需要作为需要 NSString 对象的函数的参数,我就应该将它们转换为 NSString(使用复制方法)。是这样吗?

嗯,你可以这样做,但是效率非常。记住继承——NSMutableString一个NSString,只是添加了一些新的东西。一个简单的转换就可以解决问题:

NSString *string = (NSString *)aMutableString;

更好的是,您甚至不必这样做。由于继承,您可以在需要常规字符串的地方直接传入可变字符串,无需进行强制转换。这就是传承的魅力。

当我将它们重新分配给其他常量值时,我不必处理以前的内容,对吧

对既不可变也不可变的字符串。旧值会被简单地覆盖在内存中——没有什么可以处理的。就内存管理而言,一直创建新字符串确实效率不高。只需重新分配它们即可。您永远不需要多次alloc/init一个字符串,并且单个init应该由单个release<来平衡/代码>。

附录:什么时候应该使用 Mutable?
当您物理更改现有字符串的值而不完全丢弃旧值时,应使用可变字符串。示例可能包括在开头或结尾添加字符,或更改中间的字符。使用可变字符串,您可以“就地”执行此操作 - 只需修改现有字符串即可。相比之下,不可变字符串一旦设置了其值,就无法更改该值。 NSString 具有诸如 stringByAppendingString: 之类的方法,它确实将字符串添加到现有字符串中,但它返回一个新字符串。在幕后,NSString 已将旧字符串复制到新的(更大)内存位置,添加参数并返回新字符串。这种复制的效率要低很多(相对而言,或者如果你必须做很多)。

当然,没有什么可以阻止您将一个字符串物理分配给另一个字符串。旧值将被覆盖。大多数NSString,包括@"String Constants",都是自动释放的。如果您正在创建一个新字符串并决定分配/初始化,则可以将其分配给另一个值而不会产生任何后果:

myString = anotherString;
myString = myTextField.text;

您可以使用可变和不可变字符串来执行此操作。主要的要点是,只有在更改字符串本身时才应该使用可变的。但是您可以使用可变和不可变字符串更改变量,而不会出现编译器或运行时问题(缺少内存管理,但其中大部分都是自动释放的)。

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 regular NSString. 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.

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?

Well, you could do it that way, but it would be really inefficient. Remember inheritance—an NSMutableString is an NSString, just with some new stuff tacked on. A simple cast will do the trick:

NSString *string = (NSString *)aMutableString;

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.

When I reassign them to other constant values, I shouldn't have to dispose the previous content, right

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 single init should be balanced by a single release.

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 as stringByAppendingString:, 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 NSStrings, 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:

myString = anotherString;
myString = myTextField.text;

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

情魔剑神 2024-11-25 10:39:45

对于 NSString 对象,如果我需要的话
给它们分配一个新的值,我想我
应该取消分配它们,分配它们
再次,然后分配新值。
这是正确的吗?

如果您之前没有分配过 NSString,则不会释放它,如下所示:

NSString *string = [NSString stringWithFormat:@"Hello"];

您只需要在调用 alloc 时释放它:

NSString *string = [[NSString alloc] initWithString:@"Hello"];
[string release];

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?

You don't deallocate NSString if you didn't allocated it before, like here:

NSString *string = [NSString stringWithFormat:@"Hello"];

You only need to deallocate it when you call alloc:

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