做了实验之后,我发现了一些问题,如下:
第一组:
.h
@interface DetailViewController : UIViewController {
UILabel *_myLabel;
}
@property (nonatomic, retain) UILabel *myLabel;
.m
@implementation DetailViewController
@synthesize myLabel = _myLabel;
。。。
//使用这种方式进行赋值的话,它们的retainCount都是1
- (void)viewDidLoad {
[super viewDidLoad];
_myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
NSLog(@"%d %p", [self.myLabel retainCount], self.myLabel);
NSLog(@"%d %p", [_myLabel retainCount], _myLabel);
}
。。。
@end
第二组:
.h
@interface DetailViewController : UIViewController {
}
@property (nonatomic, retain) UILabel *myLabel;
.m
@implementation DetailViewController
@synthesize myLabel;
。。。
//使用这种方式进行赋值的话,它们的retainCount也都是1
- (void)viewDidLoad {
[super viewDidLoad];
myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
NSLog(@"%d %p", [self.myLabel retainCount], self.myLabel);
NSLog(@"%d %p", [myLabel retainCount], myLabel);
}
。。。
@end
我就在想,是否可以考虑去掉实例变量的声明,因为上述的两组对比中,第二组没有声明实例变量,却达到了相同的效果,也没有内存方面的担忧,是否有达人回答下我的疑问?
浙公网安备 33010602011771号