我有以下類:我初始化了兩次嗎?
@interface Object2D : NSObject
{
Point2D* position;
Vector2D* vector;
FigureType figure;
CGSize size;
}
@property (assign) Point2D* position;
@property (assign) Vector2D* vector;
@property (assign) CGSize size;
...
@end
及其實施:
@implementation Object2D
@synthesize position;
@synthesize vector;
@synthesize size;
- (id)init
{
if (self = [super init])
{
position = [[Point2D alloc] init];
vector = [[Vector2D alloc] init];
size.width = kDefaultSize;
size.height = kDefaultSize;
}
return self;
}
當我創建的Object2D
一個實例,我這樣做:
- (void) init
{
// Create a ball 2D object in the upper left corner of the screen
// heading down and right
ball = [[Object2D alloc] init];
ball.position = [[Point2D alloc] initWithX:0.0 Y:0.0];
ball.vector = [[Vector2D alloc] initWithX:5.0 Y:4.0];
}
我不知道我是否正在初始化兩個對象Point2D
和兩個對象Vector2D
,因爲我創建了一個Point2D實例和Object2D init方法中的Vector2d。
@class Vector2D;
@interface Point2D : NSObject
{
CGFloat X;
CGFloat Y;
}
@interface Vector2D : NSObject
{
CGFloat angle;
CGFloat length;
Point2D* endPoint;
}
類Object2D,Point2D和Vector2D沒有dealloc方法。
有什麼建議嗎?
我已經更新了添加'Object2D'實現的問題。屬性具有分配屬性。 – VansFannel 2011-06-15 14:43:20
當你創建一個Object2D的實例並調用ball.position = etc等的時候,當你將這些變量分配給其他對象時,仍然創建兩個實例,並且你的init方法中的分配也會泄漏,那麼原始對象就是泄漏。 – 2011-06-15 14:51:55
我不會在init方法中初始化這些對象。 – VansFannel 2011-06-15 17:46:19