我正在开发 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];
方法 1 和 2 都有效,但是这两种方法有什么区别(方法 2 的代码更少)?
我应该使用哪种方法,为什么?
它与内存管理有关吗?