Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot init a class object : objective-C

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];
}
like image 911
Aeisys Avatar asked Aug 14 '14 00:08

Aeisys


2 Answers

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

like image 179
cream-corn Avatar answered Oct 20 '22 15:10

cream-corn


If you are here just like me wondering why you code is crashing.

 [NSArray init];

should be :

[[NSArray alloc] init];

or

[NSArray array];

You crash could caused by any other class here NSArray here is for reference only.

like image 1
Vigneshwaran.m Avatar answered Oct 20 '22 14:10

Vigneshwaran.m