14

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];
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Aeisys
  • 367
  • 1
  • 3
  • 13

2 Answers2

36

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

cream-corn
  • 1,820
  • 14
  • 26
  • Do I have to deallocate my objects later on? I'm confused by what the ARC does and doesn't handle. – Aeisys Aug 14 '14 at 00:43
  • 1
    sure, [Beginning iOS programming by Ray Wenderlich](http://www.raywenderlich.com/tutorials) is a really good resource. And any book that is less then a year old will really help too. Objective-c has matured a lot since the "old" days. ARC you generally do not have to worry about, IF you are using properties, properties do a lot for you for free. ARC (automatic reference counting) is exactly that, if a property is marked as `Strong` then it will increment the reference count, When that count becomes zero, your object will be free'd – cream-corn Aug 14 '14 at 00:49
1

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.