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]; ...
随机推荐
- vscode——配置终端集成bash和cmd
前言 配置后bash和cmd是集成的,输入bash回车则进入bash,输入cmd回车则进入cmd 步骤 首先肯定是需要打开我们的vscode咯~ 进入终端设置 配置shell路径 根据自己的系统来复制 ...
- ewasm项目初探
为了改进EVM1.0,以太坊的新一代虚拟机项目ewasm (github.com/ewasm)将支持WebAssembly(wasm),wasm在性能,扩展性,开发工具,社区都更有优势.除以太坊外,一 ...
- 第四届蓝桥杯C++B组省赛
1.高斯日记 2.马虎的算式 3.第39级台阶 4.黄金连分数 5.前缀判断 6.三部排序 7.错误票据 8.翻硬币 9.带分数 10.连号区间数
- poj 2406 Power Strings(kmp求一个串的重复子串)
题意:重复子串次数 思路:kmp #include<iostream> #include<stdio.h> #include<string.h> using nam ...
- Android多国语言文件夹汇总
Arabic, Egypt (ar-rEG) —————————–阿拉伯语,埃及 Arabic, Israel (ar-rIL) ——————————-阿拉伯语,以色列 Bulgarian, Bulg ...
- 【Codeforces 776B】Sherlock and his girlfriend
[题目链接] 点击打开链接 [算法] 将所有质数染成1,合数染成2即可 [代码] #include<bits/stdc++.h> using namespace std; #define ...
- 【NOI 2005】 维修数列
[题目链接] 点击打开链接 [算法] 本题所运用的也是Splay的区间操作,但是实现较为困难 INSERT操作 将pos splay至根节点,pos+1 splay至根节点的右 ...
- Git如何删除自己创建的项目
版本管理器第二行最右边,找到倒三角,下面的Edit Project,拖动鼠标到最下面,Remove project ,弹出框Confirmation required里面输入项目名字,如项目名字为“w ...
- Cocos2d-X对常用Object-C特性的替换
平台的转换,总是让我们不自觉的去寻找两者的相同处,不过Cocos2d-X的确对很多Object-C的特性进行了模仿性质的封装,使熟悉Object-C的人能够在其中看到很多类似的概念而感到亲切. ...
- ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 24. Logging
常用的诊断中间件: UseDeveloperExceptionPage UseStatusCodePages:返回 400~600 的状态码 UseExceptionHandler 自定义异常的处理器 ...