2010-02-09 22 views

cevap

111
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. 
    } 
} 

ben varsa hata ile yararlı bir şeyler yapmak size bırakıyoruz.

+1

: böylece bu yüzden böyle bir şey olmazdı yerine yöntemini contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error: kullanabilirsiniz NSURL kullanmak istiyorsanız

"The preferred way to specify the location of a file or directory is to use the NSURL class"

bu olmalıdır yorum, bir düzenleme değil – abatishchev

+0

Yine de yanlış olduğu gibi geri aldım. – coneybeare

+20

Genelde, yolları birleştirmek için 'stringWithFormat' yerine' stringByAppendingPathComponent 'işlevini kullanmalısınız. (Yukarıdaki işleri biliyorum, ancak sadece, "@" "Fotoğraflar /".) 'Deki sabit kodlanmış eğik çizginiz yüzünden. – zekel

16

sonra dosya ve dizin kendisini kaldırmak hızlı severler için

NSFileManager *fm = [NSFileManager defaultManager]; 
NSString *directory = [[self documentsDirectory] stringByAppendingPathComponent:@"Photos"]; 
NSError *error = nil; 
BOOL success = [fm removeItemAtPath:cacheImageDirectory error:&error]; 
if (!success || error) { 
    // something went wrong 
} 
12

aynı for döngü olmadan kullanmak istiyorsanız:

let fm = FileManager.default 
do { 
    let folderPath = "...my/folder/path" 
    let paths = try fm.contentsOfDirectory(atPath: folderPath) 
    for path in paths 
    { 
    try fm.removeItem(atPath: "\(folderPath)/\(path)") 
    } 
} catch { 
    print(error.localizedDescription) 
} 
+0

O olmamalı –

0

Swift 4

do { 

     let destinationLocation:URL = ... 

     if FileManager.default.fileExists(atPath: destinationLocation.path) { 
      try! FileManager.default.removeItem(at: destinationLocation) 
     } 

    } catch { 
    print("Error \(error.localizedDescription)") 
    } 
+0

, sadece biraz modifikasyon gerekli bana teşekkür için teşekkürler çalıştı Eğer catch kullanıyorsanız, deneyin sonra zorlayın. Ayrıca, tüm dosyaları yoluna göre URL'ye göre kaldırmaya çalışıyorsunuz. –

+0

Apple'dan: "Dosya sisteminin geçerli durumuna veya dosya sistemindeki belirli bir dosyaya göre davranışı yordama girişimi önerilmez", yani dosyanın kaldırılmadan önce var olup olmadığını kontrol etmemelisiniz. Dosyayı denemeyi ve silmeyi ve ardından hatayı uygun şekilde işlemeyi denemeniz gerekir. Daha fazla bilgi için [documentExists (atPath:) 'üzerindeki dokümanlara [https://developer.apple.com/documentation/foundation/filemanager/1415645-fileexists] bakın. – strikerdude10

0

Daha eski cevapların çoğukullanıyor çalışacakfakat according to Apple: m1neral @

NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSArray<NSURL*> *urls = [fileManager contentsOfDirectoryAtURL:directoryURL includingPropertiesForKeys:@[NSURLNameKey, NSURLIsDirectoryKey] options:NSDirectoryEnumerationSkipsHiddenFiles error:nil]; 

    for (NSURL *url in urls) 
    { 
     NSError *error = nil; 
     BOOL success = [fileManager removeItemAtURL:url error:error]; 
     if (!success || error) { 
      // something went wrong 
     } 
    } 
İlgili konular