NSString中initWithString和initWithFormat的区别
来源:互联网 发布:淘宝大学商学院杭州 编辑:程序博客网 时间:2024/06/08 07:50
NSString * str =[[NSString alloc] initWithString:@"this is from initWithString function"];
NSLog(@"this is from [[NSString alloc] initWithString] m_addr is %ld retainCount is %i", str, [str retainCount]);
[str release];
[str release];
[str release];
NSLog(@"this is from [[NSString alloc] initWithString] m_addr is %ld retainCount is %i", str, [str retainCount]);
str = [[NSString alloc] initWithFormat:@"this is from initWithFormat function"];
NSLog(@"this is from [[NSString alloc] initWithFormat] m_addr is %ld retainCount is %d", str, [str retainCount]);
retainCount
Returns the receiver’s reference count. (required)
Return Value
The receiver’s reference count.
Discussion
You might override this method in a class to implement your own reference-counting scheme. For objects that never get released (that is, their release
UINT_MAX
, as defined in <limits.h>
.
小生窃以为,此处的UINT_MAX和NSIntegerMax是一样的,都是表示所在类型的最大值。所以,initWithString这个方法初始化后的对象是不可能被release的或者说,它的release方法啥也不干。
验证了上面的分析2.因为他不可能被release的或者说,它的release方法啥也不干,所以我们调用无数次release都没有起到预先想想的作用。
为什么会导致这样的情况呢?
我们再次把目光转向地址。眼尖的同学可能会看到initWithString申请的地址每次都是一样的,而initWithFormat的地址每次都不一样,这个说明什么?
说明initWithString的地址是静态的,而initWithFormat是动态的。为什么前者是静态的,而后者是动态的?
结合上面关于retainCount的分析,小生窃以为initWithString的地址申请是在编译是进行的,这样才能说明为什么它的地址空间如此靠前。只有在编译是进行的,他才是静态的。
对于initWithString生成的对象,对其进行dealloc时,程序会报错(这里就不贴LOG了)。而后者initWithFormat不会报错。这进一步验证了initWithString生成的是静态对象,而initWithFormat是动态的。
结论:initWithString生成的对象是在编译是申请地址空间,而且在程序中不能释放,不建议使用。
- NSString中initWithString和initWithFormat的区别
- ios:initWithString和initWithFormat的区别
- initWithFormat与initWithString的区别、initWithFormat与stringWithFormat的区别
- OC中 NSString引用计数器和initWithFormat:(NSString *)string中String的存储区
- initWithFormat 和stringWithFormat的区别
- stringWithFormat与[NSString alloc]initWithFormat 区别
- iOS: NSString的方法initWithFormat:locale:arguments:
- MRC下的 initWithFormat: 和 stringWithFormat:
- NSString中使用copy 和 strong的区别?
- iOS: NSString应该用initWithFormat? 还是 stringWithFormat?
- NSString应该用initWithFormat? 还是 stringWithFormat?
- NSString应用copy和strong的区别
- initWithString与stringWithString的异同?
- iOS中NSString 使用 copy 和 strong 修饰的区别详解
- iOS中NSString和NSMutableString的用法
- objective-C: NSString应该用initWithFormat? 还是 stringWithFormat?
- objective-C: NSString应当用initWithFormat? 还是 stringWithFormat?
- Objective-C NSString应该用initWithFormat? 还是 stringWithFormat?
- POJ 3253 哈夫曼树
- el表达式跟ognl表达式的区别(用法和页面取值)
- LeetCode(307) Range Sum Query - Mutable
- imx6 板卡移植官方yocto版本(1_安装环境)
- C/C++知识点
- NSString中initWithString和initWithFormat的区别
- 软件测试目的
- uclinux 与 linux的区别
- oc集合类的创建
- 函数重用和函数引用结合测试
- git 常用命令整理
- 基于rails的schedule网站开发(8):撤销操作
- ASP.NET中使用UpdatePanel实现局部异步刷新方法和攻略
- zzuli 1068《二进制数》