【发布时间】:2012-12-31 15:58:28
【问题描述】:
请解决这个问题。
NSString *S1 = @"abc";
//retain count of s1 ??
NSString *S2 = [S1 copy];
//retain count of s2 and s1 ??
NSString *S3 = [S2 copy];
//retain count of s3 and s2 ??
NSString *S4 = [S3 retain];
//retain count of s4 and s3 ??
NSString *S1 = @"xyz";
//retain count of s1, s2, s3 and s4 ??
不要建议retainCount,因为我认为这并不能给出确切的结果。
【问题讨论】:
-
这闻起来像家庭作业。你认为答案是什么?
-
retainCount返回一个完全精确的结果。这可能不是您所期望的。 -
NSLog(@"\nretain count=%d",[obj retainCount]);一路返回-1..!! -
没错。保留计数不可靠、不可预测,最重要的是完全没有价值。不要在生产代码中发布它。
-
Your objects are constant strings and will never get deallocated.
copy或retain无论调用多少次都不会影响对象的内存管理。
标签: objective-c memory-management copy retaincount