ios - 下面提到的 NSStrings 分配的保留计数是多少

标签 ios objective-c nsstring release

在这里,我以十种不同的方式分配了 NSString 变量,我想知道所有这些变量的保留计数。

@interface SomeClass : NSObject 
{ 
   NSString *str1; 
   NSString *str2;
} 
@property (nonatomic, retain) NSString* str1;
@property (nonatomic, copy) NSString * str2; 


 - str1 =@"hello";

 - self.str1 = @"hello";

 - str1 = [[NSString alloc]init];

 - self.str4 = [[NSString alloc]init];

 - str1 = [[[NSString alloc]init]autorelease];

 - self.str1 = [[[NSString alloc]init]autorelease];

 - str1 = [[NSString alloc]initWithString:@"hello"];

 - self.str1 = [[NSString alloc]initWithString:@"hello"];

 - str1 = [[[NSString alloc]initWithString:@"hello"]autorelease];

 - self.str1 = [[[NSString alloc]initWithString:@"hello"]autorelease];

上面提到的 NSString 分配的保留计数是多少?我怎么知道它们的保留计数对于所有这些保留计数都是不同的?

最佳答案

虽然这看起来像是一项家庭作业,但您可以对每个字符串调用 retainCount 以获得真实值的近似值。您绝对不应该将此方法用于生产应用程序中的任何逻辑(参见 http://whentouseretaincount.com)!文档指出:

Special Considerations

This method is of no value in debugging memory management issues. Because any number of framework objects may have retained an object in order to hold references to it, while at the same time autorelease pools may be holding any number of deferred releases on an object, it is very unlikely that you can get useful information from this method.

关于ios - 下面提到的 NSStrings 分配的保留计数是多少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16096220/

相关文章:

ios - – drawInRect :withAttributes: crashes when setting UIFont

objective-c - 将数学方程的 NSString 转换为值

ios - 通过快速警报更改屏幕

ios - 发布 NSNotification 时更新自定义单元 UIButton 标题

ios - 如何选择以编程方式创建的文本字段?

cocoa - stringWithFormat 或 stringFromDate

ios - 无法在 Objective-C 项目中使用 RAMAnimatedTabBarController?

ios - 使用 Countly 从最终二进制文件中排除 IDFA

objective-c - 单击主页按钮后 AppDelegate 值不会更改

ios - respondsToSelector 无法识别已实现的选择器