2012-08-27 85 views
1

我是iOS的新手。我有個問題。iOS:無法刪除文件

我記錄了文件的路徑,我也在Finder中驗證它。但fileExistsAtPath:返回NO,這就是爲什麼我不能刪除它。

我需要幫助!

下面是代碼:

+ (void)removeImage:(NSString*)imgName { 
MyLog(@"%@", [Tool getFileFullPath:imgName]); 
NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSError *error; 
BOOL fileExists = [fileManager fileExistsAtPath:(NSString *)[Tool getFileFullPath:imgName]]; 
NSLog(@"Path to file: %@", [Tool getFileFullPath:imgName]);   
NSLog(@"File exists: %d", fileExists); 
NSLog(@"Is deletable file at path: %d", [fileManager isDeletableFileAtPath:[Tool getFileFullPath:imgName]]); 
if (fileExists) 
{ 
    BOOL success = [fileManager removeItemAtPath:[Tool getFileFullPath:imgName] error:&error]; 
    if (!success) NSLog(@"Error: %@", [error localizedDescription]); 
} 

} 

文件的路徑:/Users/vibolteav/Library/Application Support/iPhone Simulator/5.1/Applications/FD57CA70-14E4-442D-9CA5-DE7A7AD56A93/Documents/img/2053871632

文件存在:0

是可刪除的文件的路徑:1

+2

能否請您粘貼的路徑? – saadnib

+0

是位於沙箱中的文件嗎? – BergP

+1

是您的文件readOnly還是兩個讀寫?和你正在使用什麼代碼刪除文件?粘貼完整的代碼 –

回答

1

用於刪除文件從文件目錄你使用下面的代碼:

-(void)removeOneImage:(NSString*)fileName 
{ 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:fileName]; 
    NSLog(@"%@",fullPath); 
    [fileManager removeItemAtPath: fullPath error:NULL]; 

}

+0

我發現我的錯誤,謝謝你們所有人! – Frozenna

+0

你願意與我們分享嗎?我也有同樣的問題。 – Neeku

1

文件路徑..

NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    documentPath_ = [searchPaths objectAtIndex: 0]; 

附加文件名稱...

NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:fileName]; 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    if ([fileManager fileExistsAtPath:fullPath]) 
     { 
      NSError *error; 

      if (![fileManager removeItemAtPath:fullPath error:&error]) { 

      } 
     }