使用NSKeyedUnarchiver加载已保存状态时的EXC_BAD_ACCESS

时间:2013-11-13 19:09:39

标签: cocos2d-iphone nsuserdefaults exc-bad-access nskeyedunarchiver

我已经将CCSprite子类化为一个可以编码和解码的对象。我想保存sprite状态并在特定位置再次加载它。除了使用NSKeyedUnarchiver(请参阅下面的loadIconSpriteState)进行解码之外,其他所有内容似乎都没问题,其中EXC_BAD_ACCESS下面是代码:

HelloWorldLayer.h

@interface CCIconSprite : CCSprite {
   NSString *iconName;
   float iconXPos;
   float iconYPos;
}

@property (nonatomic, retain) NSString *iconName;
@property (nonatomic, assign) float iconXPos;
@property (nonatomic, assign) float iconYPos;

+ (id)iconWithType:(NSString*)imageName;
- (id)initWithIconType:(NSString*)imageName;
@end

@interface HelloWorldLayer : CCLayer < NSCoding>
{
    CCIconSprite* testSprite;
    BOOL savedState;
    CGSize size;
    CCMoveTo* moveTo;
    NSMutableArray* saveSpriteArray;
    NSData* savedSpriteData;

}
+(CCScene *) scene;

@end

HelloWorldLayer.m:

CCIconSprite实施:

@implementation CCIconSprite
@synthesize  iconXPos;
@synthesize iconYPos;
@synthesize iconName;

+ (id)iconWithType:(NSString*)imageName
{
    return [[[[self class] alloc] initWithIconType:imageName] autorelease];
}

- (id)initWithIconType:(NSString*)imageName
{
   self = [super initWithFile:imageName];
   if (!self) return nil;

   iconName = imageName;

   self.position = ccp(iconXPos, iconYPos);

   return self;
}

编码和解码:

- (id)initWithCoder:(NSCoder *)decoder {
   NSString* imageFileName = [decoder decodeObjectForKey:@"imageFileName"];
   self = [self initWithIconType:imageFileName];
   if (!self) return nil;

   self.iconXPos = [decoder decodeFloatForKey:@"iconXPos"];
   self.iconYPos = [decoder decodeFloatForKey:@"iconYPos"];

   return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder
{
   [encoder encodeObject:iconName forKey:@"imageFileName"];
   [encoder encodeFloat:self.iconXPos forKey:@"iconXPos"];
   [encoder encodeFloat:self.iconYPos forKey:@"iconYPos"];
}

@end

HelloWorldLayer实现:

@implementation HelloWorldLayer
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
HelloWorldLayer *layer = [HelloWorldLayer node];
[scene addChild: layer];
    return scene;
}

-(id) init
{
if( (self=[super init]) ) {
    self.scale = 0.5;
    savedState = NO;

    size = [[CCDirector sharedDirector] winSize];

    testSprite = [CCIconSprite spriteWithFile:@"Icon.png"];
    testSprite.position = ccp(size.width/4, size.width/4);
    testSprite.anchorPoint = ccp(0,0);
    [self addChild:testSprite];

    moveTo = [CCMoveTo actionWithDuration:3 position:ccp(3*size.width/4, 3*size.width/4)];
    [testSprite runAction:moveTo];

    [self schedule:@selector(saveAndLoadSpriteState)];

}
return self;
}

保存和加载状态:

-(void)saveIconSpriteState  {
    saveSpriteArray = [[NSMutableArray alloc] init];
    [saveSpriteArray addObject:testSprite];
    savedSpriteData = [NSKeyedArchiver archivedDataWithRootObject:saveSpriteArray];
 }

-(void)loadIconSpriteState  {
   [NSKeyedUnarchiver unarchiveObjectWithData:savedSpriteData];
}

-(void)saveAndLoadSpriteState    {
  if ((testSprite.position.x > size.width/2) && !savedState) {
    savedState = YES;
    [self saveIconSpriteState];
}
else if ((testSprite.position.x == 3*size.width/4) && savedState) {
    savedState = NO;
    [self loadIconSpriteState];
    [testSprite runAction:moveTo];
    }
}

@end

修改

设置异常断点后,我收到以下错误

Assertion failure in -[CCIconSprite initWithFile:]

指向内线:

NSAssert(filename != nil, @"Invalid filename for sprite");

中的

-(id) initWithFile:(NSString*)filename

CCSprite.m类的方法。

1 个答案:

答案 0 :(得分:0)

只是一种预感,但也许就是这样。解码字符串时,您应该复制它,因为此时您不拥有它,也不会将其分配给strong或copy属性。

- (id)initWithCoder:(NSCoder *)decoder {
   NSString* imageFileName = [[decoder decodeObjectForKey:@"imageFileName"] copy];
   self = [self initWithIconType:imageFileName];
   if (!self) return nil;
   ...
}

如果不是这样,请设置断点并在编码和解码时验证字符串是否确实正确。