我要初始化两次吗?
|
我有以下课程:
@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;
}
当我创建ѭ2instance的实例时,我正在这样做:
- (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
对象,因为我在Object2D init方法中创建了Point2D和Vector2d的实例。
@class Vector2D;
@interface Point2D : NSObject
{
CGFloat X;
CGFloat Y;
}
@interface Vector2D : NSObject
{
CGFloat angle;
CGFloat length;
Point2D* endPoint;
}
Object2D,Point2D和Vector2D类没有dealloc方法。
有什么建议吗?
没有找到相关结果
已邀请:
2 个回复
犁攀富
是您需要的内存泄漏...
要么
艾食魄轻县
方法,即使您自己没有声明它们也是如此。我将使用Point2D类的X和Y属性,以便无需使用
方法即可更改它们,只需使用
等即可。 更笼统地说,我建议避免像在这里那样使用Objective-C对象。当您的数据可以轻松地包含在结构中时,它可以使您的代码更加简化,从而规避Objective-C方法和内存管理的疯狂世界。