我将CCSpriteBatchNode子类化为一个符合NSCoding的对象。我主要感兴趣的是CCSpriteBatchNode的字符串名称。设置断点后,我意识到对象的字符串名称总是为零。我有一种感觉,我的被覆盖的方法可能是一个促成因素,但我不是很确定。请参阅以下相关代码:
SpriteBatchNode接口:
@interface SpriteBatchNode: CCSpriteBatchNode {
NSString* batchImageName;
}
SpriteBatchNode实现:
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
答案 0 :(得分:1)
如果您不使用ARC,我会在这里看到两个问题:
除了你正在使用一种不寻常的init风格之外,常见的风格是:
if (self)
{
batchImageName = fileImage;
}
return self;
检查self为nil,然后返回nil,如果是,则有点多余。