2011-07-21 79 views
4

我的iPhone應用程序正在將照片保存到文檔文件夾中。我想在使用後刪除所有這些文件格式的文件夾。我知道如何在時間使用路徑從iphone文檔文件夾中刪除多個文件?

if ([fileMgr removeItemAtPath:filePath error:&error] != YES) 
    NSLog(@"Unable to delete file: %@", [error localizedDescription]); 

刪除一個文件,但我想刪除所有文件中一氣呵成。我所有的文件都是.png格式,我嘗試了* .png但不工作。

回答

4
/* dir: the directory where your files are */ 
NSFileManager *fm = [NSFileManager defaultManager]; 
NSError *error = nil; 
NSArray *items = [fm contentsOfDirectoryAtPath:dir error:&error]; 
if (error) { /* ... */ } 

/* delete png files */ 
for (NSString *item in items) { 
    if ([[item pathExtension] isEqualToString:@"png"]) { 
     NSString *path = [dir stringByAppendingPathComponent:item]; 
     [fm removeItemAtPath:path error:&error]; 
     if (error) 
      { /* ... */ } 
    } 
} 
+0

感謝您的幫助,我只是在條件和工作後添加以下內容。 NSString * path = [documentsDirectory stringByAppendingPathComponent:item]; [fileMgr removeItemAtPath:path error:&error]; –

+0

啊,當然。對不起拉維,我錯過了。 – sidyll

相關問題