私はXcodeを初めて使用しますが、これについては本当に助けが必要です。
アーカイブを使用してアイテムの配列を含むファイルを保存し、それらを読み取ってテーブルビューに配置しようとしています。
問題は、アプリを閉じてから再度起動すると、データをファイルに保存すると既存のファイルが上書きされ、保存されているすべてのデータが失われることです。
私はこのメソッドを使用してデータをファイルに保存しています:
-(id)init
{
if(self = [super init])
{
_appSupportPath = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES)lastObject];
if (!_appSupportPath)
return nil;
_repositoryPath = [_appSupportPath stringByAppendingPathComponent:FILE_NAME];
if (_data == nil)
_data = [[NSMutableDictionary alloc] init];
}
return self;
}
-(void)saveToFile
{
dispatch_queue_t writeQueue = dispatch_queue_create("MyApp:file:write", NULL);
NSMutableDictionary *localData = [NSMutableDictionary dictionaryWithDictionary:_data];
dispatch_async(writeQueue, ^{
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:_appSupportPath])
[fileManager createDirectoryAtPath:_appSupportPath withIntermediateDirectories:YES attributes:nil error:NULL];
[NSKeyedArchiver archiveRootObject:localData toFile:_repositoryPath];
});
}
何か助けはありますか?
私が知らないファイルを保存するためのより良い/より簡単な方法があるかどうか私に知らせてください。
前もって感謝します