开发者

When should we use NSString and NSMutableString?

I want to when should we use NSString a开发者_开发百科nd when should we use the NSMutableString, because we can append the string in NSString also,so what is the effective use of NSMutableString in Objective C programming?


I expect MutableString will be slightly more efficient in terms of memory as you are indicating early on that the program may need memory dynamically. NSString is likely to be allocated a single, suitably sized block of memory.

With NSString and its stringBy... methods, you are creating new objects, releasing the old one (if need be) and making the new object autorelease. (Take care if you are changing from non-autorelease to autorelease, you may have a release in your dealloc that isn't needed anymore)


When using NSMutableString you can do without allocation and initing following:

  1. append some string or format
  2. delete or replace character in string
  3. insert or replace strings

I think in this cases memory is only reallocated (realloc()) besides free() + alloc() + init in case of using NSString


You should use NSString when you need a string as a fixed (immutable) string value, and NSMutableString as an editable (mutable) string container (buffer).

If you're not sure, use NSString primarily. In most cases, it will offer better overall performance.

Why should I prefer NSString?

NSString's value never changes. This means you can sure once made NSString will be as is forever, whatever you do on it. So you can trust once validated string value without any additional check. This reduces program complexity and checking cost greatly.

NSMutableString doesn't have this kind of guarantee. After once you passed NSMutableString into some function or method, now you cannot know its current value. Now you need to perform validation or check again.

You can think you know what will be changed or not. But for big programs, or code from other people, it's hard to determine. Just using of NSString simplifies this a lot, so you will have less stuffs to worry about.

How the NSString is more efficient though it needs to allocate new object for each time I change the string?

For overall program, actually you don't change strings always. Actually, many of string values are not need to be changed. You do assign or copy the strings just as is a lot more. When you copy a NSMutableString it makes exact copy, and this needs CPU resource, memory access, memory space. NSString doesn't needs actual copying operation because it is not copied. This is possible because it is guaranteed not to be changed (immutable), so it just re-uses itself. Almost no cost at all.

And you should have comparably small portion of code which need heavy string editing. Use NSMutableString at there. Because NSString cannot be changed, it needs making a new object when you need some new string value. This is inefficient, and almost the only inefficient point of NSString.

The only pros of NSMutableString is it supports in-place editing. In other words, it doesn't need to make new objects when you want to make new string value. So you can save object making cost if you need to make many changes.

Cocoa designed to take advantage of this mutability separation. You will discover you need to declare @property (copy) instead of @property (strong) to ensure the object won't be affected by any changes in other place of the program. NSString is very efficient at here unlike NSMutableString needs to be copied every time.

This is same for all the NS~/NSMutable~ classes.


With NSString you only create new instances - which means allocating memory for a new object instance and losing the old instance - this is what happens when you append string to existing NSString under the hood. NSString you should use only for object which won't change their value.

With NSMutableString on the other hand you work with only one object instance - it just mutates the string data it holds. More efficient memory wise and faster.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜