J'ai la classe suivante:Suis-je en train d'initialiser deux fois?
@interface Object2D : NSObject
{
Point2D* position;
Vector2D* vector;
FigureType figure;
CGSize size;
}
@property (assign) Point2D* position;
@property (assign) Vector2D* vector;
@property (assign) CGSize size;
...
@end
Et sa mise en œuvre:
@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;
}
Lorsque je crée une instance de Object2D
, je fais ceci:
- (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];
}
Je suis Je ne sais pas si j'initialise deux objets Point2D
et deux objets Vector2D
, car je crée une instance de Point2D et Vector2d dans la méthode d'initialisation Object2D.
@class Vector2D;
@interface Point2D : NSObject
{
CGFloat X;
CGFloat Y;
}
@interface Vector2D : NSObject
{
CGFloat angle;
CGFloat length;
Point2D* endPoint;
}
Les classes Object2D, Point2D et Vector2D n'ont pas de méthode dealloc.
Un conseil?
J'ai mis à jour ma question en ajoutant 'Object2D'implementation. Les propriétés ont un attribut assign. – VansFannel
Toujours en train de créer deux instances et vos allocations dans la méthode init fuient, dès que vous affectez ces variables à d'autres objets, comme lorsque vous créez une instance de Object2D et appelez ball.position = etc etc alors les objets d'origine sont fuite. –
Je ne vais pas initialiser ces objets sur la méthode init. – VansFannel