Objective C 实例变量尽管被保留但仍被释放

发布于 2024-11-24 16:03:01 字数 833 浏览 5 评论 0原文

我有一个棉花糖类,它(除其他外)有一个 CCSprite 对象作为实例变量。

这是 init 方法:

-(id) init
{
    if((self = [super init]))
    {   
        model = [[CCSprite spriteWithFile:@"marshmallow.png"] retain];
        maxSpeed = 5; //160px per second (maxspeed * PTM_Ratio = px/second max)
        gravity = 9.81; // in meters/sec^2
        health = 3;
    }

    return self;
}

该变量在另一个文件中声明为全局变量,并使用以下行:

Marshmallow *mainChar;

稍后在文件中,使用此行设置(初始化/分配):

mainChar = [[mainChar alloc] init];

在编写上一行时,xcode 给了我一个警告 Marshmallow 可能不会响应分配。 (我不认为这是相关的。只是提到任何看起来错误的东西)

我的问题是以下代码行返回 nil:

[mainChar getModel];

为什么它返回 nil 而不是实例变量?

这是 getModel 函数:

-(CCSprite *)getModel
{
    return model;
}

I have a marshmallow class which has (among other things) a CCSprite object as an instance variable.

here is the init method:

-(id) init
{
    if((self = [super init]))
    {   
        model = [[CCSprite spriteWithFile:@"marshmallow.png"] retain];
        maxSpeed = 5; //160px per second (maxspeed * PTM_Ratio = px/second max)
        gravity = 9.81; // in meters/sec^2
        health = 3;
    }

    return self;
}

the variable is declared in another file as a global variable with the line:

Marshmallow *mainChar;

Later in the file, it is set (initiated/alloc'd) with this line:

mainChar = [[mainChar alloc] init];

while writing the previous line, xcode gave me a warning that Marshmallow might not respond to alloc. (I don't think that's related. just mentioning anything that seems wrong)

my problem is that the following line of code returns nil:

[mainChar getModel];

why does it return nil instead of the instance variable?

here is the getModel function:

-(CCSprite *)getModel
{
    return model;
}

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

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

发布评论

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

评论(3

堇色安年 2024-12-01 16:03:01
mainChar = [[mainChar alloc] init];

不应该吗

mainChar = [[Marshmallow alloc] init];

该消息表示该类的对象可能不会响应它,而不是本身。

mainChar = [[mainChar alloc] init];

Shouldn't be

mainChar = [[Marshmallow alloc] init];

?

The message says an object from that class might not respond to it, not the class itself.

时间海 2024-12-01 16:03:01

您的问题在于 mainChar 变量的初始化。您要查找的行是这样的:

mainChar = [[mainChar alloc] init];

您收到的警告告诉您 Marshmallow 类型的实例将不会响应 -alloc 消息。这就是你的问题:你想调用 +alloc 类方法,如下所示:

mainChar = [[Marshmallow alloc] init];

Your problem is in the initialization of your mainChar variable. The line you're looking for is this:

mainChar = [[mainChar alloc] init];

The warning you got is telling you that instances of type Marshmallow will not respond to the -alloc message. That is your problem: you want to call the +alloc class method instead, like so:

mainChar = [[Marshmallow alloc] init];
故事未完 2024-12-01 16:03:01

我认为你想做的

mainChar = [[MarshMallow alloc] init];

不是

mainChar = [[mainChar alloc] init];

你得到的错误消息非常重要。

I think you want to do

mainChar = [[MarshMallow alloc] init];

instead of

mainChar = [[mainChar alloc] init];

The error message you got is very important.

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