所以我实现了这两个函数来在我的应用程序中保存数据:
- (NSString*)itemArchivePath
{
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentDirectory = [documentDirectories firstObject];
return [documentDirectory stringByAppendingString:@"items.archive"];
}
-(BOOL)saveChanges
{
NSString* path = [self itemArchivePath];
return [NSKeyedArchiver archiveRootObject:self.highScores toFile:path];
}
它在 iOS 模拟器上完美地保存了所有内容,但是当我在实际 iPhone 上运行它时,它没有保存任何内容。大多数应用程序都是用 swift 编写的,但我认为这不是问题所在。而且我确实实现了 NSCoding
的协议(protocol)。
最佳答案
您构建的路径不正确。
这个:
return [documentDirectory stringByAppendingString:@"items.archive"];
需要:
return [documentDirectory stringByAppendingPathComponent:@"items.archive"];
您的原始代码生成如下路径:
some path to app/Documentsitems.archive
请注意文件名前没有斜杠。
关于ios - NSKeyedArchiver 适用于模拟器但不适用于实际设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24846859/