我初始化两次了吗?

发布于 2024-11-15 16:45:21 字数 1367 浏览 3 评论 0原文

我有以下类:

@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 对象,因为我在 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 方法。

有什么建议吗?

I have the following class:

@interface Object2D : NSObject
{
    Point2D* position;
    Vector2D* vector;
    FigureType figure;
    CGSize size;
}

@property (assign) Point2D* position;
@property (assign) Vector2D* vector;
@property (assign) CGSize size;

...

@end

And its implementation:

@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;
}

When I create an instance of Object2D, I'm doing this:

- (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];

}

I'm not sure if I'm initializing two Point2D objects and two Vector2D objects, because I make an instance of Point2D and Vector2d in Object2D init method.

@class Vector2D;

@interface Point2D : NSObject
{
    CGFloat X;
    CGFloat Y;
}


@interface Vector2D : NSObject
{
    CGFloat angle;
    CGFloat length;
    Point2D* endPoint;
}

Classes Object2D, Point2D and Vector2D don't have dealloc method.

Any advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海风掠过北极光 2024-11-22 16:45:21

是的,你是。另外,如果您在属性上有“保留”属性,那么像这样的行...

ball.position = [[Point2D alloc] initWithX:0.0 Y:0.0];

是您需要的内存泄漏...

ball.position = [[[Point2D alloc] initWithX:0.0 Y:0.0] autorelease];

或者

Point2D *point = [[Point2D alloc] initWithX:0.0 Y:0.0];
ball.position = point;
[point release];

Yes you are. Also if you have the 'retain' attribute on the properties then lines like this...

ball.position = [[Point2D alloc] initWithX:0.0 Y:0.0];

are memory leaks you need either...

ball.position = [[[Point2D alloc] initWithX:0.0 Y:0.0] autorelease];

or

Point2D *point = [[Point2D alloc] initWithX:0.0 Y:0.0];
ball.position = point;
[point release];
半暖夏伤 2024-11-22 16:45:21

是的,您正在为每个类创建两个实例。而且它们确实有内置的 dealloc 方法,即使您自己没有声明它们。我会将 X 和 Y 设置为 Point2D 类的属性,这样您就可以在没有 initWithX:Y: 方法的情况下更改它们,只需使用 aPoint.X

更一般地说,我建议避免使用 Objective-C 对象,就像您在这里所做的那样 当您的数据可以轻松包含在结构中时,它可以使您的代码更加精简,从而避开 Objective-C 方法和内存管理的疯狂世界。

Yes, you are creating two instances of each of those classes. And they do have dealloc methods built in, even if you haven't declared them yourself. I would make X and Y properties of the Point2D class so that you can change them without your initWithX:Y: method, just by using aPoint.X, etc.

More generally, I would suggest avoiding using Objective-C objects as you've done here. When your data can be easily contained in a struct, it can make your code more streamlined to dodge the crazy world of Objective-C methods and memory management.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文