简体   繁体   English

NSMutableString与NSString的用法?

[英]Usage of NSMutableString vs NSString?

I am confused between NSString and NSMutable string usage. 我在NSStringNSMutable字符串用法之间感到困惑。 Suppose I have instance variables one and two declared in class like this: 假设我在类中声明了实例变量1和2,如下所示:

NSString *one;
NSMutableString *two;

let us suppose I have created setters and getters of them using properties. 让我们假设我已经使用属性创建了它们的setter和getter。

lets say I have changed my "two" string like these: 让我说我已经改变了我的“两个”字符串,如下所示:

     1. [two appendString:@"more string"];
     2. two = @"string"
     3. self.two = @"string"

Questions: 问题:

  1. would 1st line release previous string and allocate new object and assign value to it. 将第1行释放上一个字符串并分配新对象并为其赋值。 if yes, then does that mean creating getters and setters are unnecessary in this case ? 如果是,那么这是否意味着在这种情况下创建getter和setter是不必要的? OR its unnecessary to create properties in NSMutablestring 或者它不必在NSMutablestring创建属性

  2. In this case would the previous allocated string object released ? 在这种情况下会释放先前分配的字符串对象吗?

  3. Its for sure that first object would be released as we are calling setters here. 它肯定会释放第一个对象,因为我们在这里调用setter。 is this code necessary or we can just use the code line 2 to assign string. 这个代码是必需的,或者我们可以使用代码行2来分配字符串。

Now about NSString : As can modify the string like this also : 现在关于NSString :也可以像这样修改字符串:

    one = [one stringByAppendingString:@" more string"];
    self.one = [one stringByAppendingString:@" more string"];

Which is better using NSMutablestring or NSString ? 使用NSMutablestringNSString哪个更好?

Sorry for long post, but I needed to understand these concepts. 很抱歉很长的帖子,但我需要了解这些概念。

For the first part of your question: 对于问题的第一部分:

  1. Almost definitely not. 几乎绝对不是。 I expect the memory will be allocated dynamically, rather than released and reallocated. 我希望内存将动态分配,而不是释放和重新分配。

  2. If the previous answer is no, this is also, no. 如果前面的答案是否定的,那也是,不。

  3. The second and third options don't even work with the warning Incompatible pointer types assigning NSMutableString to NSString 第二个和第三个选项甚至不能使用警告Incompatible pointer types assigning NSMutableString to NSString

I expect NSMutableString will be slightly more efficient in terms of memory as you are indicating early on that the program may need memory dynamically. 我希望NSMutableString在内存方面会稍微提高效率,因为你早就指出程序可能需要动态内存。 NSString is likely to be allocated a single, suitably sized block of memory. NSString可能被分配一个适当大小的内存块。

Your views of NSMutableString seem to be what the stringBy.. methods of NSString will do: 您对NSMutableString看法似乎是NSString的stringBy..方法将执行的操作:

With NSString and its stringBy... methods, you are creating new objects, releasing the old one (if need be) and making the new object autorelease. 使用NSString及其stringBy...方法,您将创建新对象,释放旧对象(如果需要)并使新对象自动释放。 (Take care if you are changing from non-autorelease to autorelease, you may have a release in your dealloc that isn't needed anymore) (如果您从非自动释放更改为自动释放,请注意,您可以在dealloc中释放不再需要的版本)

NSString is a not mutable string object, which means, after initialization, you cannot change it, NSMutableString is mutable meaning you can append another string to it or other modifications. NSString是一个不可变的字符串对象,这意味着,在初始化之后,您无法更改它,NSMutableString是可变的意味着您可以向其附加另一个字符串或其他修改。

when you do [two appendString:@"more string"] , the pointer still pointed to the same location in memory and you don't have to worry about allocation or deallocation. 当你执行[two appendString:@"more string"] ,指针仍指向内存中的相同位置,您不必担心分配或释放。

When you do two = @"string" , it means you make your string pointer pointed to a specific location in memory which contains static string with value "string" inside. 当你做two = @"string" ,它意味着你让你的字符串指针指向内存中的一个特定位置,该位置包含值为“string”的静态字符串。

When you do self.two = @"string" , you've already have a property named two declared. 当你做self.two = @"string" ,你已经有了一个名为two的属性。 By using property in xcode, you don't have to worry about the memory since you've already specified in your property declaration. 通过在xcode中使用属性,您不必担心内存,因为您已经在属性声明中指定了内存。 It is a nice tool xcode provide to you, and you should definitely use it whenever you can. 这是一个很好的工具xcode提供给你,你应该尽可能地使用它。

NSMutableString will save some memory as NSString objects are always constants. NSMutableString将保存一些内存,因为NSString对象始终是常量。 So reassigning a new value to an NSString variable will allocate new memory for the new string. 因此,将新值重新分配给NSString变量将为新字符串分配新内存。

However, please note a few errors in your code. 但是,请注意代码中的一些错误。 You will have to initialize the objects before sending messages to them: 在向对象发送消息之前,您必须初始化对象:

// this won't work
NSString *one; 
[one stringByAppendingString:@"more string"];

// nor this
NSMutableString *two;
[two appendString:@"more string"];

// first do this: 
NSString *one = @"an initial string constant"; // or
NSString *one = [NSString string]; 

NSMutableString *two = [NSMutableString string]; 

NSString objects are immutable this means that when you "modify" a NSString you are in fact creating a new string and not modifying the old which is not very effective. NSString对象是不可变的,这意味着当你“修改”一个NSString你实际上是在创建一个新的字符串,而不是修改那个不太有效的旧字符串。 With NSMutableString the string is kept in a buffer than can be modified. 使用NSMutableString ,字符串保存在缓冲区中,而不是可以修改的。

So the simple rule is that if you need to modify the string in any way use a NSMutableString and if not NSString . 因此,简单的规则是,如果您需要以任何方式修改字符串,请使用NSMutableString ,如果不是NSString You can also cast a NSMutableString to a NSString but not the other way around (then you need to make a copy) 你也可以将NSMutableString转换为NSString但不是相反(然后你需要复制)

The difference between NSMutableString and NSString is that : NSMutableString和NSString之间的区别在于:

NSMutableString: NSMutableString objects provide methods to modify the underlying array of characters they represent, while NSString does not. NSMutableString:NSMutableString对象提供了修改它们所代表的基础字符数组的方法,而NSString则没有。 For example, NSMutableString exposes methods such as appendString, deleteCharactersInRange, insertString, replaceOccurencesWithString, etc. All these methods operate on the string as it exists in memory.We can change string object valsue at run time. 例如,NSMutableString公开了appendString,deleteCharactersInRange,insertString,replaceOccurencesWithString等方法。所有这些方法都对内存中存在的字符串进行操作。我们可以在运行时更改字符串对象的值。

NSString: on the other hand only is a create-once-then-read-only string if you will; NSString:另一方面,如果你愿意的话,只有一个create-once-then-read-only字符串; you'll find that all of its "manipulation" methods (substring, uppercaseString, etc) return other NSString objects and never actually modify the existing string in memory. 你会发现它的所有“操作”方法(substring,uppercaseString等)返回其他NSString对象,从不实际修改内存中的现有字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM