iOS:无法删除文件

时间:2012-08-27 04:54:17

标签: iphone objective-c 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 个答案:

答案 0 :(得分: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];

}

答案 1 :(得分: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]) {

           }
       }