分配后RetainCount是-1吗? [英] RetainCount is -1 after alloc?

查看:113
本文介绍了分配后RetainCount是-1吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请不要标记重复": 我已经创建了几行

Please don't mark Duplicate:- I have created the few lines

NSString *str = [[NSString alloc] init];
str = @"Test";
NSLog(@"%d",[str retainCount]);

,输出为-1.请解释.

and Output is -1 .Please explain.

推荐答案

这是我所指出问题的重复.因为您将UINT_MAX打印为有符号整数,所以它为-1. RC为-1,因为它是编译器有效生成的单例字符串,并且从未分配或释放.

It is a dupe of the question I pointed to. It is -1 because you are printing UINT_MAX as a signed integer. The RC is -1 because that is a singleton string generated by the compiler, effectively, and is never allocated or deallocated.

此模式:

    NSString *str = [[NSString alloc] init];
    str = @"Kashyap";

毫无道理;您正在分配一个字符串实例,在第一行中将对它的引用分配给str,然后立即在第二行中覆盖该引用(如果不使用ARC,则泄漏第一字符串).

Makes no sense; you are allocating a string instance, assigning a reference to it to str in the first line, and then immediately overwriting that reference in the second line (and leaking the first string, if you aren't using ARC).

如果有提倡该反模式的教程或书籍,请指出.

If there is a tutorial or book that is advocating that anti-pattern, please point it out.

这篇关于分配后RetainCount是-1吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆