NSString的retainCount

来源:互联网 发布:类似金十数据的网站 编辑:程序博客网 时间:2024/05/22 04:43
    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]

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




原创粉丝点击