我有几个简单的问题,以确保我在我的应用程序中使用属性。我在网上阅读了很多内容,但目前还不清楚。非常感谢您的帮助或建议。iPhone的属性
(1)我不太确定该陈述是否有效,为什么需要。
@synthesize personName = _personName;
为什么你需要_personName变量?这样做有什么好处,而不仅仅是创建一个属性并合成变量personName。
@property (nonatomic, retain) NSString *personName;
(2)在我的应用程序应该访问属性变量self.personName或使用_personName变量。我相信self.personName是正确的,那么为什么_personName即使在那里? (3)另外我有点困惑,我应该在dealloc()中释放哪个变量,以及哪个变量应该在viewDidLoad()中设置为nil。我也不知道是否应该对didReceiveMemoryWarning()方法进行任何更改。
@interface ViewController : UIViewController
{
NSString *_personName;
}
@property (nonatomic, retain) NSString *personName;
@end
@implementation ViewController
@synthesize personName = _personName;
- (void)viewDidLoad
{
[super viewDidLoad];
self.personName = [[NSString alloc] initWithString:@"John Doe"];
NSLog(@"Name = %@", self.personName);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (void)dealloc
{
[super dealloc];
}
@end
对于最后一个问题:http://stackoverflow.com/questions/9276136/what-is-the-need-of-assigning-nil-after-releasing-an-object?answertab=active#tab -top – Saphrosit 2012-03-11 17:32:21
太棒了,自从那篇文章有帮助以后,我就把它拿出来了。 – Vikings 2012-03-11 17:34:45
[Synthesized属性和变量与下划线前缀:这是什么意思?]可能重复(http://stackoverflow.com/questions/6049269/synthesized-property-and-variable-with-underscore-prefix-what-does-这意味着) – 2012-03-11 18:54:33