【发布时间】:2011-05-03 11:32:15
【问题描述】:
我正在开发 iPad 游戏。我遇到这件事。这是我的示例代码:
方法一:
Foo.h
@interface Foo : UIView{
UILabel *title;
.... // Other objects like UISlider, UIbuttons, etc.
}
// I add @property for all the objects.
@property (nonatomic, retain) UILabel *title;
... blablabla
Foo.m
// I synthesize all the properties.
@synthesize title;
... blablabla
// Release in dealloc method
[title release];
....
[super dealloc];
方法二:
Foo.h
@interface Foo : UIView{
UILabel *title;
.... // Others object like UISlider, UIbuttons, etc.
}
// But this time I didn't add @property, synthesize and release.
Foo.m
// When I need the label, I allocate it:
title = [[UILabel alloc] initWithRect: CGRect(10, 10, 100, 30)];
title.text = @"test";
[self addSubview: title];
[title release];
方法一和方法二都可以,但是这两种方法有什么区别(方法二的代码少)?
我应该使用哪种方法以及为什么?
这和内存管理有关吗?
【问题讨论】:
标签: objective-c memory-management synthesizer