アプリの指定されたディレクトリ内のすべてのファイルを削除するにはどうすればいいですか? 質問する

アプリの指定されたディレクトリ内のすべてのファイルを削除するにはどうすればいいですか? 質問する

ディレクトリが指定されている場合、[[self documentsDirectory] stringByAppendingPathComponent:@"Photos/"]このフォルダ内のすべてのファイルを削除するにはどうすればよいですか?

(正しいドキュメント ディレクトリ パスを想定)

ベストアンサー1

NSFileManager *fm = [NSFileManager defaultManager];
NSString *directory = [[self documentsDirectory] stringByAppendingPathComponent:@"Photos/"];
NSError *error = nil;
for (NSString *file in [fm contentsOfDirectoryAtPath:directory error:&error]) {
    BOOL success = [fm removeItemAtPath:[NSString stringWithFormat:@"%@%@", directory, file] error:&error];
    if (!success || error) {
        // it failed.
    }
}

エラーが存在する場合、それを利用して何か役に立つことをするのはあなた次第です。

おすすめ記事