我正在開發iPad遊戲。我遇到這件事。這是我的示例代碼:@property,@synthesize並釋放Objective-C中的對象
方法1:
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];
方法2:
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具有更少的代碼)?
我應該使用哪種方法,爲什麼?
它與內存管理有關嗎?
重複:[1](http://stackoverflow.com/questions/2032826/)[3](http://stackoverflow.com/questions/5140782/)[3](http://stackoverflow.com/questions/3169822 /)[4](http://stackoverflow.com/questions/4700296/)[5](http://stackoverflow.com/questions/3394206/)[&c。](http:// stackoverflow .com/search?page = 2&tab = relevance&q = synthesize%20iphone)當你發佈信息時,這些都會出現在「相關問題」中。發帖前請先搜索。 – 2011-05-03 20:02:31