0

据我所知,如果对象的保留计数变为 0,则调用它的 dealloc。但我将保留计数设为 -1。这是什么意思?

我正在使用以下代码-

    Demo *obj1 = [[Demo alloc] init];
    NSString *s = [[NSString alloc] initWithString:@"mithilesh"];
    NSString *s1 = [NSString stringWithString:s];
    [s release];
    object_setInstanceVariable(obj1, [propertyName cString], s1);
    //printing retain count 
    NSLog(@"retain count of name = %i",[obj1.name retainCount]);

输出 :

   retain count of name = -1

stringWithString方法返回的字符串:何时发布?

4

1 回答 1

11

这意味着你需要停止调用-retainCount。它纯粹是一个调试功能,除非您是从事系统框架或编译器工作的 Apple 工程师,否则您可能没有任何业务可以查看该功能。

也就是说,您在此处看到的内容INT_MAX被解释为签名数字。听起来您可能已经掌握了一个常量字符串@"mithilesh"。常量字符串是不参与-retainand的单例, -releasea -retainCountofINT_MAX表示这一点。但这实际上只是猜测,您应该停止查看-retainCount

如果你不相信我,也许你会相信 Bill Bumgarner 说retainCount 是无用的

于 2012-02-14T07:44:34.350 回答