NSString 与NSMutableString的区别

 

NSString 与NSMutableString的区别 

 

Suppose You have a code like this

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

s = [s stringByAppendingString:@"World"];

 

假如NSString是这样

and other code like this

NSMutableString *ms = [[NSMutableString alloc] initWithString:@"Hello"];

[ms appendString:@"World"];

另一个可变的String是这样。

Both of these, functionally, do the same thing except for one difference - the top code block leaks

-[NSString stringByAppendingString:] generates a new immutable NSString object which you then tell the pointer s to point to.

[NSString stringByAppendingString:]产生一个新的不可变的NSString 对象,而这个对象就是你要的指针要指向的对象。

In the process, however, you orphan the NSString object that s originally pointed to. Replacing the line as follows would get rid of the leak:

s = [[s autorelease] stringByAppendingString:@"World"];

The difference between NSMutableString and NSString is that

NSMutableString: NSMutableString objects provide methods to modify the underlying array of characters they represent, while NSString does not. For example, NSMutableString exposes methods such as appendStringdeleteCharactersInRangeinsertString,replace Occurrences With String, etc. All these methods operate on the string as it exists in memory.

NSMutalbeString对象提供方法修改他们代表的字符串数组,而NSString不能。例如

NSMutalbeString提供了appendString ,deleteCharactersInRange,insertString ,replace occurrences with String。所有的方法都在 它已经存在的内存上操作

NSString: on the other hand only is a create-once-then-read-only string if you will; 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.

NSString :另一方面,NSString是一个创建一次,只读一次。你会发现所有的操作方法(substring,uppercaseString,等)返回另外的NSStirng对象,实际上,从来不会修改已经存在的对象。

 

 

When To Use Mutable Strings

Since NSString and NSMutableString provide such similar functionality, it can be hard to know when to use one over the other. In general, the static nature of NSString makes it more efficient for most tasks; however, the fact that an immutable string can’t be changed without generating a new object makes it less than ideal when you’re trying to perform several small edits.

一般情况下使用NSString 可以更有效,然而实际情况是immutable string 如果不是创建一个新的对象的话,就不能做修改。

The two examples presented in this section demonstrate the advantages of mutable strings. First, let’s take a look at an anti-pattern for immutable strings. The following loop generates a string containing all of the numbers between 0 and 999 using NSString.

// DO NOT DO THIS. EVER.
NSString *indices = @"";
for (int i=0; i<1000; i++) {
    indices = [indices stringByAppendingFormat:@"%d", i];
}

Remember that stringByAppendingFormat: creates a new NSStringinstance, which means that in each iteration, the entire string gets copied to a new block of memory. The above code allocates 999 string objects that serve only as intermediary values, resulting in an application that requires a whopping 1.76 MB of memory. Needless to say, this is incredibly inefficient.

stringByAppendingFormat:创建一个新的NSString 实例,意味着每次迭代,整个string 都会被拷贝到新的内存空间上去。上面的代码分配了999个string 对象。这开销很大的。

Now, let’s take a look at the mutable version of this snippet:

NSMutableString *indices = [NSMutableString stringWithCapacity:1];
for (int i=0; i<1000; i++) {
    [indices appendFormat:@"%d", i];
}

Since mutable strings manipulate their contents in place, no copying is involved, and we completely avoid the 999 unnecessary allocations. Internally, the mutable string’s storage dynamically expands to accommodate longer values. This reduces the memory footprint to around 19 KB, which is much more reasonable.

因为mutable strings 操作他们的内容在原来的位置,不需要拷贝,我们完全避免了不需要的999分配。

 

 

时间: 2024-11-20 11:54:36

NSString 与NSMutableString的区别的相关文章

strong retain copy对于 nsstring,nsmutablestring的区别

#import "ViewController.h" @interface ViewController ()@property (retain,nonatomic) NSString *myRetainStr;@property (copy, nonatomic) NSString *myCopyStr;@property (strong, nonatomic) NSString *myStrongStr;@end @implementation ViewController - (

Foundation框架-NSString和NSMutableString

可变与不可变的字符串 --1-- Foundation框架介绍 1.1 框架介绍 --2-- NSString 2.1 NSString介绍及使用 2.2 NSString创建方式  2.3 从文件中读取/存储字符串 2.4 字符串的比较 2.5 前后缀检查及搜索 2.6 字符串的截取和替换 2.7 获取字符串的每个字符 2.8 字符串和其他数据类型转换 2.9 NSString去除空格  --3-- NSMutableString 3.1 NSMutableString 基本概念 3.2 字符

关于NSString和NSMutableString的相关用法和基本介绍

Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重新给这个字符串赋值.而NSMutableString 创建赋值以后可以动态在该字符串上更改内容与长度. NSString 常用方法总结 +(id)stringWithContentsOfFile:path encoding:enc err 创建一个新字符串并将其设置为 path 指定文件的内容,使用

黑马程序员-OC学习笔记之NSString与NSMutableString

---------------------- IOS开发.Android培训.期待与您交流! ---------------------- 一.NSString与NSMutableString 相信大家对NSString类都不陌生,它是OC中提供的字符串类,它的对象中的字符串都是不可变的,而它的子类NSMutableString类的对象中的字符串就是可变的.什么是可变与不可变呢?二者的区别就是在已经创建的一个字符串对象中,在程序中能不能更改字符串,比如拼接新的字符串,可变的可以在原字符串中更改,

NSString和NSMutableString 的常见用法

NSString类是我们经常使用的类,这里对其用途及常用的一些方法做一些归类. 1>字符串的创建 NSString *str = @"字符串内容“;   //直接使用字符串常量的赋值方式 NSString *str = [NSString alloc ] init];   //创建一个空的字符串对象 NSString *str = [NSString stringWithFormat: @""];  //使用类方法的格式化创建方式来创建字符串 NSString *str

Objective-C之NSString和NSMutableString

Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重新给这个字符串赋值.而NSMutableString 创建赋值以后可以动态在该字符串上更改内容与长度. +(id) stringWithContentsOfFile:path encoding:enc error:err创建一个新字符串并将其设置为path指定的文件的内容,使用字符编码enc,如果非

bjective-C 中核心处理字符串的类是 NSString 与 NSMutableString

Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重新给这个字符串赋值.而NSMutableString 创建赋值以后可以动态在该字符串上更改内容与长度. 1.创建经典的NSString字符串 NSString 与 char* 最大的区别就是 NSString是一个objective对象,而char* 是一个字节数组.@+ " 字符串 " 

Foundation框架(NSString、NSMutableString)

OC字符串(NSString.NSMutableString) 概述:在Object C中存在两个类用于操作字符串,NSString和NSMutableString;NSString在赋值之后不能修改其内容和长度,而NSMutableString可以动态的修改字符串内容和长度. 一.NSString 1.NSString的多种定义方式 1)使用@符号快速创建一个字符串 NSString *str1 = @“jack”; 2)使用对象初始化方法 initWithString  NSString *

NSString 和 NSMutableString

// //  main.m //  字符串(NSString&NSMutableString) // //  Created by 闫合 on 16/5/19. //  Copyright © 2016年 闫合. All rights reserved. // #pragma mark NSString 和 NSMutableString #import <Foundation/Foundation.h> int main(int argc, const char * argv[])