NSString的retainCount

    NSString *str1 = [[NSString alloc] initWithString:@"111"];
    //[NSString stringWithString:@"111"];
    //@"111";
    NSLog(@"str1%@:retainCount:%d", str1, [str1 retainCount]);


通过上面3种方式创建出来的NSString的retainCount 都是-1

不可变?静态常量区?(_NSCFConstantString *)?


NSString *temp = @"/tmp/scratch";
Note that, when creating a string constant in this fashion, you should avoid using anything but 7-bit ASCII characters. Such an object is created at compile time and exists throughout your program’s execution. The compiler makes such object constants unique on a per-module basis, and they’re never deallocated, though you can retain and release them as you do any other object.

// FROM http://www.cnblogs.com/Piosa/archive/2012/03/05/2380096.html


    // -------------------------------------------------------------------------

    // 结论I

    // 普通NSString

    // copy = retain [同一对象 retainCount+1,不可修改]

    // mutableCopy [新对象 retainCount = 1,可修改]

    //

    // 可变NSMutableString 

    // copy = mutableCopy [新对象 retainCount = 1]

    // copy != mutableCopy [copy=不可修改, mutableCopy=可修改]

    //  

    // NSString @Property使用COPY,而不用retain

    // 可以避免隐式NSMutableString被外部修改

    // 

    // 结论II

    // initWithString和直接赋值的NSString [retainCount = -1]

    // 编译时就已经生成在静态常量区,并一直存在。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值