gpt4 book ai didi

ios - NSKeyedArchiver 在模拟器上工作但不在实际设备上工作

转载 作者:行者123 更新时间:2023-11-29 02:56:07 27 4
gpt4 key购买 nike

我似乎无法让 NSKeyedArchiver 工作。我有一个主 ViewController,它有一个 NSMutableArray 作为实例变量。该数组由自定义类的多个实例填充。自定义类符合NSCoding协议(protocol),并实现了编码和解码方法。从主视图 Controller 中,我调用

- (NSString *)itemArchivePath
{
NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

//only one document in list - get path t o it
NSString *documentDirectory = [documentDirectories firstObject];

return [documentDirectory stringByAppendingString:@"locations.archive"];
}

- (BOOL)saveChanges
{
//returns success or failure
NSString *path = [self itemArchivePath];

return [NSKeyedArchiver archiveRootObject:myArray //this is the array to archive
toFile:path];
}

在应用程序委托(delegate)中,我调用 applicationDidEnterBackground 的 saveChanges 方法。该方法在模拟器上返回 true,但在实际设备上返回 false。我不知道该怎么办。任何帮助表示赞赏。谢谢!

编辑:根据 Zaph 的评论,我已将 saveChanges 方法更改为:

- (void)saveChanges
{
//returns success or failure
NSString *path = [self itemArchivePath];

NSData *dataOfArray = [NSKeyedArchiver archivedDataWithRootObject:myArray];
NSFileManager *checker = [NSFileManager defaultManager];
[dataOfArray writeToFile:path
atomically:YES];

if ([checker fileExistsAtPath:path]) {
NSLog(@"Archive Successful");

}
else {
NSLog(@"Archive Did Not Work");
}

}

最佳答案

NS记录路径!正如@Wolfgang 所说,这可能是错误的。

[documentDirectory stringByAppendingString:@"locations.archive"]

不会在路径和文件名之间添加“/”。使用:

[documentDirectory stringByAppendingPathComponent:@"locations.archive"]

当代码不起作用时,请使其尽可能简单,将复合语句分解为单独的语句。然后使用调试器和/或 NSLog() 语句检查每一步。

关于ios - NSKeyedArchiver 在模拟器上工作但不在实际设备上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23909373/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com