加载数据 EXC BAD ACCESS(仅限设备)

发布于 2024-10-21 08:23:37 字数 1347 浏览 2 评论 0原文

当我的自定义数据加载到 iPhone(3G,3.1)上时,我收到 Exc Bad Access 在这一行中:

NSMutableArray* dataArr = [NSKeyedUnarchiver unarchiveObjectWithFile:pathGG]; //=EXC BAD ACCESS

在 Ipad 上,在模拟器上工作

我在模拟器中保存数据(到文件目录路径),然后将数据替换到项目中,并从 [NSBundle mainBundle] 加载

在数组中我使用 NSValue 来存储 CGPoint 。

完整来源:

-(void) SaveData:(NSMutableArray*)dataLevel {
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* fileName = @"data.dat";
NSString* pathGG = [documentsDirectory stringByAppendingPathComponent:fileName]; // retain];

BOOL isWrite = [NSKeyedArchiver archiveRootObject:dataLevel toFile:pathGG];

if(isWrite) NSLog(@"YES");  
else  NSLog(@"!!!");
}

+(NSMutableArray*) LoadData  {
NSString* fileName = @"data.dat"; 
NSString* pathGG =  [[NSBundle mainBundle] pathForResource:fileName ofType:@"dat"]; // retain];
NSMutableArray* dataA = [NSKeyedUnarchiver unarchiveObjectWithFile:pathGG];  //EXC BAD ACCESS

return dataA;
}

如果我使用以下方式(保存/加载相同的方法),我也会得到 EXC BAD ACCESS :

NSKeyedUnarchiver* decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
NSArray* dataArr = [decoder decodeObjectForKey:@"dataLevel"]; //EXC BAD ACCESS

When my custom data is loading on iPhone (3G, with 3.1) I get Exc Bad Access
in this line:

NSMutableArray* dataArr = [NSKeyedUnarchiver unarchiveObjectWithFile:pathGG]; //=EXC BAD ACCESS

On Ipad, and on simulator work

I saving data in simulator (to documentsDirectory path), then replace data to project, and load from [NSBundle mainBundle]

In array I use NSValue for store CGPoint.

Full source:

-(void) SaveData:(NSMutableArray*)dataLevel {
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* fileName = @"data.dat";
NSString* pathGG = [documentsDirectory stringByAppendingPathComponent:fileName]; // retain];

BOOL isWrite = [NSKeyedArchiver archiveRootObject:dataLevel toFile:pathGG];

if(isWrite) NSLog(@"YES");  
else  NSLog(@"!!!");
}

+(NSMutableArray*) LoadData  {
NSString* fileName = @"data.dat"; 
NSString* pathGG =  [[NSBundle mainBundle] pathForResource:fileName ofType:@"dat"]; // retain];
NSMutableArray* dataA = [NSKeyedUnarchiver unarchiveObjectWithFile:pathGG];  //EXC BAD ACCESS

return dataA;
}

If I am using follow way(saving/loading same method), I get EXC BAD ACCESS too:

NSKeyedUnarchiver* decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
NSArray* dataArr = [decoder decodeObjectForKey:@"dataLevel"]; //EXC BAD ACCESS

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜警司 2024-10-28 08:23:38

试试这个:

NSString* pathGG = [[documentsDirectory stringByAppendingPathComponent:fileName] retain];

然后当你完成它时:

[pathGG release]

Try this:

NSString* pathGG = [[documentsDirectory stringByAppendingPathComponent:fileName] retain];

Then when you are finished with it:

[pathGG release]
满意归宿 2024-10-28 08:23:38

解决方案:不要将 CGPoint 存储在 NSValue 中,而是使用 CGPointFromString(在 NSStringFromCGPoint 之后),然后将字符串添加到数组中。

NSString* positionStr = NSStringFromCGPoint(someCGPoint); 
[someArray addObject:position];
// Now, saving array to file
...

//从文件加载数组后

CGPoint positon = CGPointFromString([someArr objectAtIndex:0]);                  

SOLUTION: Instead of store CGPoint in NSValue, use CGPointFromString(after NSStringFromCGPoint), and then add string to array.

NSString* positionStr = NSStringFromCGPoint(someCGPoint); 
[someArray addObject:position];
// Now, saving array to file
...

//After loading array from file

CGPoint positon = CGPointFromString([someArr objectAtIndex:0]);                  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文