当应用程序通过TestFlight从iTunes下载时,NSKeyedArchiver问题



我有一个非常奇怪的bug,几天来我一直在努力追踪它。我正在将游戏的状态数据保存在应用程序Documents目录中的文件中。

在最近的iOS更新之前,一切都很好(不确定确切的时间,大约在9.0左右)。突然间,数据没有正确存档/取消存档。

奇怪的是,当我用连接到MAC的iPad从Xcode运行代码时,或者在模拟器中运行时,代码运行得很好。当我使用TestFlight从iTunes下载该应用程序时,它不再工作。这使得调试变得极其困难。

我已经检查了所有内容,我正在使用URL路径,添加了错误捕获代码等,但当应用程序通过TestFlight从iTunes安装时,存档无法正常工作。

作为最后的手段,我添加了新的代码来归档我的对象,立即将其解压缩到另一个变量中,然后在标签中显示一些数据。生成的对象包含null数据。

没有抛出异常。

只是重申一下,只有当应用程序从iTunes安装时,代码才起作用。

以下是代码片段;

NSString *documentDirectory = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] path];
NSString* filePath =  [documentDirectory stringByAppendingPathComponent:@"playerTest.data"];
LBYPlayerData* pd1 = [[LBYPlayerData alloc ]init];
pd1.currentCountryID = 1;
pd1.lsn = @"123.456";
BOOL success = [NSKeyedArchiver archiveRootObject:pd1 toFile:filePath];
NSAssert(success, @"archiveRootObject failed");
LBYPlayerData* pd2 = nil;
@try {
pd2 = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
} @catch (NSException *exception) {
playerDataLabel.text = [NSString stringWithFormat:@"%@",exception.name];
playerUndoDataLabel.text = [NSString stringWithFormat:@"%@",exception.description];
} @finally {
NSAssert((pd2 != nil), @"archivePlayerDataUndo failed to unarchive");
playerDataLabel.text = [NSString stringWithFormat:@"path: %@",filePath];
playerUndoDataLabel.text = [NSString stringWithFormat:@"Undo Country:%li LSN:%@",(long)pd2.currentCountryID,pd2.lsn];
}

这是的数据模型

//
//  LBYPlayerData.h
#import <Foundation/Foundation.h>
@interface LBYPlayerData : NSObject
@property (nonatomic,readonly) BOOL isNewGame;
@property (nonatomic) NSInteger playerID;
@property (nonatomic) NSInteger usCardIdx;
@property (nonatomic) NSInteger drawDeckIdx;
@property (nonatomic) NSInteger discardDeckIdx;
@property (nonatomic) NSInteger removeDeckIdx;
@property (nonatomic) NSInteger currentCountryID;
@property (nonatomic) NSString* lsn;
@property (nonatomic) NSString* build;
@end
//
//  LBYPlayerData.m
#import "LBYPlayerData.h"
@implementation LBYPlayerData
-(id)init
{
self = [super init];
_isNewGame = YES;
return self;
}
-(void)encodeWithCoder:(NSCoder *)aCoder
{
//    NSLog(@"Saving Player Data");
_isNewGame = NO;
[aCoder encodeBool:_isNewGame         forKey: NSStringFromSelector(@selector(isNewGame))];
[aCoder encodeInt64:_playerID         forKey: NSStringFromSelector(@selector(playerID))];
[aCoder encodeInt64:_usCardIdx        forKey: NSStringFromSelector(@selector(usCardIdx))];
[aCoder encodeInt64:_drawDeckIdx      forKey: NSStringFromSelector(@selector(drawDeckIdx))];
[aCoder encodeInt64:_discardDeckIdx   forKey: NSStringFromSelector(@selector(discardDeckIdx))];
[aCoder encodeInt64:_removeDeckIdx    forKey: NSStringFromSelector(@selector(removeDeckIdx))];
[aCoder encodeInt64:_currentCountryID forKey: NSStringFromSelector(@selector(currentCountryID))];
[aCoder encodeObject:_lsn             forKey: NSStringFromSelector(@selector(lsn))];
[aCoder encodeObject:_build           forKey: NSStringFromSelector(@selector(build))];
//    NSLog(@"Current Counry: %li",(long)_currentCountryID);
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
//    NSLog(@"Loading Player Data");
self = [self init];
if (self) {
_isNewGame               =[aDecoder decodeBoolForKey:NSStringFromSelector(@selector(isNewGame))];
[self setPlayerID        :[aDecoder decodeIntegerForKey:NSStringFromSelector(@selector(playerID))]];
[self setUsCardIdx       :[aDecoder decodeIntegerForKey:NSStringFromSelector(@selector(usCardIdx))]];
[self setDrawDeckIdx     :[aDecoder decodeIntegerForKey:NSStringFromSelector(@selector(drawDeckIdx))]];
[self setDiscardDeckIdx  :[aDecoder decodeIntegerForKey:NSStringFromSelector(@selector(discardDeckIdx))]];
[self setRemoveDeckIdx   :[aDecoder decodeIntegerForKey:NSStringFromSelector(@selector(removeDeckIdx))]];
[self setCurrentCountryID:[aDecoder decodeIntegerForKey:NSStringFromSelector(@selector(currentCountryID))]];
[self setLsn             :[aDecoder decodeObjectForKey :NSStringFromSelector(@selector(lsn))]];
[self setBuild           :[aDecoder decodeObjectForKey :NSStringFromSelector(@selector(build))]];
}
return self;
}
@end

问题出在NSAssert上。我在NSAssert语句中找到了调用函数来归档对象的代码。

最新更新