データベースをNSBundleからドキュメントディレクトリにコピーしたいとします。NSBundleからドキュメントディレクトリにファイルをコピーできません
これは、ファイルをコピーするためのコードです:
- (NSString *) getDBPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingString:@"Mydata.sqlite"];
}
- (void) copyDatabaseIfNeeded
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success) {
NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Mydata.sqlite"];
success = [fileManager copyItemAtPath:defaultPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, @"Failed to create writable file with message '%@'.", [error localizedDescription]);
}
}
をしかし、なぜ、それは私が私のiPadでこのアプリケーションを実行すると、それがクラッシュするということでしょうか?
エラーメッセージは何ですか? – Nekto
私はそれがクラッシュする理由を見つけました! 問題はここにあります。return [documentsDir stringByAppendingString:@ "Mydata.sqlite"]; 代わりにstringByAppendingPathComponentを使用する必要があります。 – Lloydworth