0

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

1 Answers1

1

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.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • I am using ARC and I changed the init style to the one above but I am still getting the same thing in the log - `batchImageName = (NSString*) nil` – errorallergic Nov 17 '13 at 04:11
  • When and where do you log? Is it nil when encoding or after decoding? In the latter case post the code that initiates the decode. – CodeSmile Nov 17 '13 at 16:39