无法初始化一个类对象:objective-C

Aei*_*sys 8 subclass objective-c init cocos2d-iphone

我正在尝试创建一个简单的CCNode子类,但我无法创建该对象.

它给出了错误" *因未捕获的异常而终止应用程序'NSInvalidArgumentException',原因:'* + [ContentPane <0x206898> init]:无法初始化类对象.'"

这是我的CCNode的子类:

#import "ContentPane.h"

@implementation ContentPane{
    int content[8][4];
    CCSprite *_rockPath1;
    CCSprite *_rockPath2;
}

- (id)init {
    self = [super init];

    if (self) {
        CCLOG(@"ContentPane created");
    }

    return self;
}
Run Code Online (Sandbox Code Playgroud)

@结束

这是我尝试启动它的地方:

- (void)didLoadFromCCB {
    // tell this scene to accept touches
    self.userInteractionEnabled = TRUE;
    _counter = 0;
    ContentPane *pane = [ContentPane init];
}
Run Code Online (Sandbox Code Playgroud)

cre*_*orn 23

情侣,

在Obj-c中,当您想要初始化Object时,需要为它分配空间.这是使用alloc关键字完成的.

所以你的 ContentPane *pane = [ContentPane init];

变成 ContentPane *pane = [[ContentPane alloc] init];

此外,无论您遵循什么教程,停止...您声明变量的方式我们称之为(iVars)是一种非常老式的做事方式,它们应该是属性.和Boolean值由YES和表示而NO不是TRUEFALSE