<分区>
<分区>
这是我的代码:
@interface YQViewController ()
@property (nonatomic, strong) UILabel *lb1;
@end
@implementation YQViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = @"title";
self.lb1 = [[UILabel alloc]init];
NSLog(@"retain count is %ld", CFGetRetainCount((__bridge CFTypeRef)self.lb1));
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
如您所见,我将 +alloc
和 -init
发送到 self.lb1
,我认为 的保留计数self.lb1
应等于 1,但控制台输出为 2。有人能告诉我原因吗?(ARC 启用,xcode 5,OSX 10.9.1,iOS 7 模拟器)。
最佳答案
如果您正在查找 retainCount
的值,那您就错了。也不异常(exception)。您不能依赖结果值来反射(reflect)代码中发生的事情。
有关详细信息,请参阅 http://whentouseretaincount.com
关于启用 ARC 的 iOS 保留计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22030390/