NSString 与NSMutableString的区别
NSString *s = [[NSString alloc] initWithString:@"Hello"];
s = [s stringByAppendingString:@"World"];
NSMutableString *ms = [[NSMutableString alloc] initWithString:@"Hello"];
s = [[s autorelease] stringByAppendingString:@"World"];The difference between NSMutableString and NSString is that
When To Use Mutable Strings
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.// DO NOT DO THIS. EVER.
NSString*indices=@"";
for(inti=0;i<1000;i++){
indices=[indicesstringByAppendingFormat:@"%d",i];
}
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.Now, let’s take a look at the mutable version of this snippet:
NSMutableString*indices=[NSMutableStringstringWithCapacity:1];
for(inti=0;i<1000;i++){
[indicesappendFormat:@"%d",i];
}
NSString 与NSMutableString的区别的更多相关文章
- Foundation框架-NSString和NSMutableString
可变与不可变的字符串 --1-- Foundation框架介绍 1.1 框架介绍 --2-- NSString 2.1 NSString介绍及使用 2.2 NSString创建方式 2.3 从文件中 ...
- bjective-C 中核心处理字符串的类是 NSString 与 NSMutableString
Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重 ...
- 关于NSString和NSMutableString的相关用法和基本介绍
Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重 ...
- NSString和NSMutableString常用方法+NSArray常用代码 (转)
常见的NSString和NSMutableString方法: NSString方法: [plain] view plaincopy +(id) stringWithContentsOfFile:p ...
- iOS基础-NSString及NSMutableString剖析
一.NSString头文件 NSString : NSObject 实现协议: NSCopying/NSMutableCopying/NSSecureCoding 类别: //扩展类别 NSStrin ...
- NSString和NSMutableString的创建及其一些常用方法
NSString和NSMutableString都是对象类型,是NSObject的子类.NSString是不可变字符串,NSMutableString是可变字符串 一.NSString的创建 1.创建 ...
- Objective-C: 字符串NSString与NSMutableString
字符串算是OC中非常重要和常用的一部分内容,OC中的字符串与我之前在学习C,C++,Java中的字符串有一定的不同,它非常类似于C++中容器的概念,但用法却与之还是有很大的不同,也许是因为OC的语法就 ...
- Objective-C学习篇06—NSString与NSMutableString
NSString OC提供了定义字符串对象的方法,也就是将想要表达的字符串用一对双引号引起来,并在开头加上@.@是OC中的指令符,它告诉编译器@以后的内容为OC中的语法.比如@”Harbingwang ...
- KZ--NSString、NSMutableString
//NSString初始化的几种方法(3种方法) //1. NSString *str2 = [[NSString alloc] init]; ...
随机推荐
- Pyhton:List build-in function
列表是Python中的可迭代对象之一,在讲列表的内建函数之前我们可以自己在IDE上看看都有那些内建函数,我们可以在pycharm中使用代码及其运行结果如下: print(dir(list)) ['__ ...
- codeforces C. Magic Formulas 解题报告
题目链接:http://codeforces.com/problemset/problem/424/C 题目意思:给出 n 个数:p1, p2, ..., pn,定义: q1 = p1 ^ (1 mo ...
- Excel: 应用Match/Vlookup比较Excel两列的不同数据
假设Excel中有两列,现在要比较两列数据的不同.
- 分析Android (build/core/*.mk脚本)
文档简要整理Android的make脚本的内容.以供备忘和参考. 1. Build LayersBuild Layers描述的是产品的硬件配置情况,据此make时选择不同的配置和模块.按照从上到 ...
- 在 Ubuntu 系统中有三种设置环境变量 PATH 的方法。(ZT) repost
来源地址: http://blog.csdn.net/jernymy/article/details/6547671 第一种适用于为单一用户设置PATH.第二种是为全局设置 PATH.第三种方法适合于 ...
- 使用git rebase合并多次commit
使用git rebase合并多次commit 聊下 git rebase -i
- Table View Programming Guide for iOS---(四)---Navigating a Data Hierarchy with Table Views
Navigating a Data Hierarchy with Table Views 导航数据表视图层次 A common use of table views—and one to which ...
- 1-1 课程导学 & 1-2 项目需求分析,技术分解.
1-1 课程导学 1-2 项目需求分析,技术分解. 要有一定的dart基础,了解安卓和ios的一些普通的开发
- Swift3.0 元组 (tuples)
//元组 //不需要的元素用 _ 标记 let (name,age,_) = (","男") print(name,age) //通过下标访问特定的元素 let stud ...
- Navicat Premium连接服务器数据库
解决Navicat 连接服务器失败的问题 由于服务器的安全问题,有些东西默认是关闭的.就像远程连接服务器的数据库一样,如果默认是每个IP都能访问,安全性就会大大降低,甚至没有安全性可言.但是由于项目需 ...