1

我是iOS的新手。我有个问题。

我记录了文件的路径,并在 Finder 中进行了验证。但是 fileExistsAtPath: return 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

4

2 回答 2

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]) {

           }
       }
于 2012-08-27T05:38:21.717 回答
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];

}

于 2012-08-27T05:13:29.520 回答