アプリケーションの起動時にNSBundleからDocumentsディレクトリに複数のファイルをコピーしたいと思います。 「applicationDidFinishLaunchingWithOptions」メソッドに複数のファイルをDocumentsディレクトリにコピー
- (NSString *) getPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingString:@"Photo.png"];
}
- (void) copyFile
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *Path = [self getPath];
BOOL success = [fileManager fileExistsAtPath:Path];
if(!success) {
NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Photo.png"];
success = [fileManager copyItemAtPath:defaultPath toPath:Path error:&error];
if (!success)
NSAssert1(0, @"Failed to create writable file with message '%@'.", [error localizedDescription]);
}
}
CopyFileメソッドが呼び出されます。
この
は、ファイルをコピーするためのコードです。しかし、この方法では、1つのファイルをDocumentsディレクトリにコピーすることしかできません。 NSBundleから50個のファイルをコピーしたい場合は、50個のパスを指定する必要がありますか?コードのほんの数行でNSBundleのすべてのファイルを取得するなどの方法がありますか?
なぜか分かりますか? – Lloydworth
@Lloydworthそれで、あなたは 'copyFileWithName'を50回呼び出すことができるので、毎回別のファイル名を与え、' copyFileWithName'は毎回 'getPathOfFileName'を別のファイル名で呼び出して、コピーされた現在のファイルのパスを取得できます。もちろん、これはまだあなたが50個のファイルすべてのリストを必要としていることを意味します。だから、これは本当に便利な解決策ではありません。 – Mecki