iPhoneの開発を始めたばかりです。私がタブバコントローラのテーブルビューにsqlite dbに格納されたデータを表示しなければならない例の1つでは、sqliteファイルをアプリケーションバンドルからドキュメントフォルダに移動しなければなりませんでした。iPhone:ファイルからリソースにファイルをコピーする方法は?
Iは、アプリケーションテンプレート使用 - 、XCodeの(最新のiOS = 4.2に設定し、ベースSDK)によって生成されたテンプレートで
(記憶のためにコアデータを使用し)iPhone用のiOS>アプリケーション>ウィンドウベースのアプリケーションを、以下のコードはドキュメントフォルダのパスを取得しようとしているではあり...
- (NSURL *)applicationDocumentsDirectory {
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
だった、私はこのような上に与えられた方法を使用...
NSString *documentDirectory = [self applicationDocumentsDirectory];
これはwarniを与えましたNG - warning: incompatible Objective-C types initializing 'struct NSURL *', expected 'struct NSString *'
だから私は...次へ
// Added the message absoluteString over here
NSString *documentDirectory = [[self applicationDocumentsDirectory] absoluteString];
NSString *writableDBPath = [documentDirectory stringByAppendingPathComponent:@"mydb.sqlite"];
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"mydb.sqlite"];
BOOL success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSLog(@"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
とブームを、コードを変更! - 'Failed to create writable database file with message 'The operation couldn’t be completed. No such file or directory'.'
XCodeテンプレートで生成されたapplicationDocumentsDirectory
というメソッドが私のために動作しないため、ドキュメントディレクトリへのパスをどのように見つけるべきですか?
また、誰かが上に与えたapplicationDocumentsDirectory
の目的のためにいくつかの光を投げることができます。
おかげ
私はwritableDBPath上のファイル名を追加していなかったので、私はエラーを取得しました。この例は、私がそれを修正するのを助けました。ありがとう! +1 – voghDev