【问题标题】:unable to delete my file in ios [duplicate]无法在ios中删除我的文件[重复]
【发布时间】:2013-10-30 07:31:48
【问题描述】:

我正在尝试删除特定 URL 处的文件, 我的代码是:

    NSString *str= [outputFieldURL absoluteString];

       NSError *error;

        BOOL success = [[NSFileManager defaultManager] removeItemAtPath:str error:&error];



        if (!success) {
            NSLog(@"Error removing file at path: %@", error.localizedDescription);
        }
        else
        {
            NSLog(@"File removed  at path: %@", error.localizedDescription);
        }
    }

并得到输出: 删除路径处的文件时出错:操作无法完成。 (可可错误4。)

当我将 outputFieldURL 转换为字符串时,它在哪里显示以下值:
file:///var/mobile/Applications/A55A56FA-478D-4996-807D-12F0E968F969/Documents/301013125211w.m4a

这是保存我的 .m4a 格式音频的路径

【问题讨论】:

    标签: ios cocoa nsfilemanager


    【解决方案1】:

    你的路径不正确

    使用下面的

    NSString *str= [outputFieldURL path];
    

    代替

    NSString *str= [outputFieldURL absoluteString];
    

    “removeItemAtPath:”方法需要文件的本地路径, 如果你想使用 url 删除,你应该使用 "removeItemAtURL:"

    【讨论】:

      【解决方案2】:

      您提供的文件路径可能不正确。如果正确,请尝试使用 URL,它可能会解决您的问题

          NSString *str= [outputFieldURL absoluteString];
      
          NSError *error;
      
          NSURL *url = [NSURL URLWithString:str];
          BOOL success = [[NSFileManager defaultManager] removeItemAtURL:url error:&error];
      
      
          if (!success) {
              NSLog(@"Error removing file at path: %@", error.localizedDescription);
          }
          else
          {
              NSLog(@"File removed  at path: %@", error.localizedDescription);
          }
      }
      

      【讨论】:

      • 请阅读上述答案(NEHA提到)。它有效。
      猜你喜欢
      • 1970-01-01
      • 2021-08-28
      • 2020-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-04
      • 2013-10-03
      • 2021-12-24
      相关资源
      最近更新 更多