在这里,我以十种不同的方式分配了 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/