문서 디렉토리에 포함 된 모든 파일과 디렉토리를 삭제하고 싶습니다.Documents 디렉터리의 내용을 삭제하는 방법 (Documents 디렉터리 자체가 아닌)
[fileManager removeItemAtPath : documentsDirectoryPath error : nil] 메서드를 사용하면 문서 디렉토리도 제거됩니다.
디렉토리의 내용 만 삭제하고 빈 디렉토리는 그대로 둘 수있는 방법이 있습니까?
문서 디렉토리에 포함 된 모든 파일과 디렉토리를 삭제하고 싶습니다.Documents 디렉터리의 내용을 삭제하는 방법 (Documents 디렉터리 자체가 아닌)
[fileManager removeItemAtPath : documentsDirectoryPath error : nil] 메서드를 사용하면 문서 디렉토리도 제거됩니다.
디렉토리의 내용 만 삭제하고 빈 디렉토리는 그대로 둘 수있는 방법이 있습니까?
이 시도 :
NSString *folderPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSError *error = nil;
for (NSString *file in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error:&error]) {
[[NSFileManager defaultManager] removeItemAtPath:[folderPath stringByAppendingPathComponent:file] error:&error];
}
스위프트 3.x를
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
guard let items = try? FileManager.default.contentsOfDirectory(atPath: path) else { return }
for item in items {
// This can be made better by using pathComponent
let completePath = path.appending("/").appending(item)
try? FileManager.default.removeItem(atPath: completePath)
}