cannot init a class object : objective-C

Aeisys picture Aeisys · Aug 14, 2014 · Viewed 9.5k times · Source

I'm trying to make a simple subclass of CCNode, but I can't create the object.

It gives me the error "* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* +[ContentPane<0x206898> init]: cannot init a class object.'"

Here is my subclass of 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;
}

@end

Here is where I try to initiate it:

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

Answer

cream-corn picture cream-corn · Aug 14, 2014

Couple things,

In Obj-c when you want to initialize an Object, you need to allocate space for it. That is done using the alloc keyword.

so your ContentPane *pane = [ContentPane init];

turns into ContentPane *pane = [[ContentPane alloc] init];

Also, whatever tutorial you are following, Stop... the way you have declared your variables we call them (iVars) is a very old fashioned way of doing things, they should really be properties. and Boolean values are represented by YES and NO not TRUE and FALSE