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分配。
 
 
 
 
 
 

NSString 与NSMutableString的区别的更多相关文章

  1. Foundation框架-NSString和NSMutableString

    可变与不可变的字符串 --1-- Foundation框架介绍 1.1 框架介绍 --2-- NSString 2.1 NSString介绍及使用 2.2 NSString创建方式  2.3 从文件中 ...

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

    Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重 ...

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

    Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重 ...

  4. NSString和NSMutableString常用方法+NSArray常用代码 (转)

    常见的NSString和NSMutableString方法: NSString方法: [plain] view plaincopy   +(id) stringWithContentsOfFile:p ...

  5. iOS基础-NSString及NSMutableString剖析

    一.NSString头文件 NSString : NSObject 实现协议: NSCopying/NSMutableCopying/NSSecureCoding 类别: //扩展类别 NSStrin ...

  6. NSString和NSMutableString的创建及其一些常用方法

    NSString和NSMutableString都是对象类型,是NSObject的子类.NSString是不可变字符串,NSMutableString是可变字符串 一.NSString的创建 1.创建 ...

  7. Objective-C: 字符串NSString与NSMutableString

    字符串算是OC中非常重要和常用的一部分内容,OC中的字符串与我之前在学习C,C++,Java中的字符串有一定的不同,它非常类似于C++中容器的概念,但用法却与之还是有很大的不同,也许是因为OC的语法就 ...

  8. Objective-C学习篇06—NSString与NSMutableString

    NSString OC提供了定义字符串对象的方法,也就是将想要表达的字符串用一对双引号引起来,并在开头加上@.@是OC中的指令符,它告诉编译器@以后的内容为OC中的语法.比如@”Harbingwang ...

  9. KZ--NSString、NSMutableString

            //NSString初始化的几种方法(3种方法)         //1.         NSString *str2 = [[NSString alloc] init];      ...

随机推荐

  1. vue开发:移动端图片上传

    因为最近遇到个移动端上传头像的需求,上传到后台的数据是base64位,其中为了提高用户体验,把比较大的图片用canvas进行压缩之后再进行上传.在移动端调用拍照功能时,会发生图片旋转,为了解决这个问题 ...

  2. 2016-5-23 jsp

    1.table的边框:rules这个参数,它有三个值(cols,rows,none),当rules=cols时,表格会隐藏横向的分隔线,也就是我们只能看到表格的列:当rules=rows时,就隐藏了纵 ...

  3. BM算法模式匹配——字符串与KMP比较

    下面是代码:BM是什么参考阮一峰老师的讲解  点击打开链接 #include<iostream> #include<algorithm> #include<string. ...

  4. C#方法参数总结

    C#中方法的参数的四种类型 C#中方法的参数有四种类型:       1. 值参数类型  (不加任何修饰符,是默认的类型)       2. 引用型参数  (以ref 修饰符声明)       3. ...

  5. 从OutStreamWriter 和Filewriter谈Java编码

    首先看JAVA API的描述: ABOUT OutputStreamWriter: "An OutputStreamWriter is a bridge from character str ...

  6. SPOJ:String Play (?)

    String Play Milo has a string S of length L. Tutu picks a random prefix and Mota picks a random suff ...

  7. CentOS 6.5升级到CentOS 7

    CentOS7 已经发布了,之前一直想在上面测试一下,一直没有机会,这次终于可以感受一下CentOS7了.一直使用CentOS6.5有一段时间了,但是由于它的内核版本依然停留在2.6.32,所以决定升 ...

  8. npm 脚本

    查看安装的包: npm list -g --depth 0 考虑到用CLI这种方式来运行本地的webpack不是特别方便,我们可以设置一个快捷方式,在package.json添加一个npm脚本(npm ...

  9. 为什么选择SSM+Redis框架开发模式?

    1.选择spring 目前企业的java应用中,spring框架是必须的,spring的核心是IOC(控制反转),它是一个大容器,方便组装和管理各类系统内外部资源,同时支持AOP(控制反转),这是对面 ...

  10. 洛谷P2515 [HAOI2010]软件安装(tarjan缩点+树形dp)

    传送门 我们可以把每一个$d$看做它的父亲,这样这个东西就构成了一个树形结构 问题是他有可能形成环,所以我们还需要一遍tarjan缩点 缩完点后从0向所有入度为零的点连边 然后再跑一下树形dp就行了 ...