加载数据 EXC BAD ACCESS(仅限设备)
当我的自定义数据加载到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
然后当你完成它时:
Try this:
Then when you are finished with it:
解决方案:不要将
CGPoint
存储在NSValue
中,而是使用CGPointFromString
(在NSStringFromCGPoint
之后),然后将字符串添加到数组中。//从文件加载数组后
SOLUTION: Instead of store
CGPoint
inNSValue
, useCGPointFromString
(afterNSStringFromCGPoint
), and then add string to array.//After loading array from file