errorallergic
errorallergic

Reputation: 105

Subclassed CCSpriteBatchNode object is nil

I subclassed CCSpriteBatchNode to make an object that conforms to NSCoding. I was mainly interested in the string name of the CCSpriteBatchNode. After setting break points I realized that the object's string name is always nil. I have a feeling it that my overridden methods might be a contributing factor but I'm not really sure about that. Please see relevant code below:

SpriteBatchNode interface:

@interface SpriteBatchNode: CCSpriteBatchNode {
NSString* batchImageName;
}

SpriteBatchNode implementation:

const NSUInteger defCapacity = 29;

@implementation SpriteBatchNode

@synthesize batchImageName;

+(id)batchNodeWithFile:(NSString*) imageFile
{
return [[self alloc] initWithFile:imageFile capacity:defCapacity];
}

-(id)initWithFile:(NSString *)fileImage {
self = [super initWithFile:fileImage capacity:defCapacity];

if (!self) return nil;

batchImageName = fileImage;

return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder
{
NSString* spriteBatchNodeFileImage = [[aDecoder decodeObjectForKey:@"batchImageName"] copy];

self = [super initWithFile:spriteBatchNodeFileImage capacity:defCapacity];
if (!self) return nil;

return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder
{
   [aCoder encodeObject:batchImageName forKey:@"batchImageName"];
}

@end

Upvotes: 0

Views: 55

Answers (1)

CodeSmile
CodeSmile

Reputation: 64477

If you aren't using ARC I see two problems here:

  • batchImageName string is not retained
  • batchNodeWithFile: is not sending autorelease to the returned instance

Other than that you're using an unusual init style, the common style is this:

if (self)
{
    batchImageName = fileImage;
}
return self;

Checking self for nil, then returning nil if it is, is somewhat redundant.

Upvotes: 1

Related Questions