菜鸟教程小白 发表于 2022-12-11 16:40:49

ios - 删除文件名中带有空格的文件 ios


                                            <p><p>我想删除文档目录的图像。该图像具有这种 <code>2016-06-08 12:24:55.897image.jpg</code> 类型的命名约定。</p>

<p>代码片段</p>

<pre><code>-(void) removeImageAtPath:(NSString *) filePath{
NSError *error;
NSFileManager *fileManager = ;
BOOL success = ;
if (success) {
    NSLog(@&#34;Image Successfully Deleted&#34;);
}
else{
    NSLog(@&#34;Could not delete file -:%@ &#34;,);
}
}
</code></pre>

<p>错误代码
<code>NSCocoaErrorDomain 代码 = 4</code></p>

<p>我知道找不到文件时会出现错误。这是由于我使用的命名约定而发生的。
我不能改变约定。有什么办法仍然可以删除文件。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>此代码直接删除文件名</p>

<pre><code> - (void)removeImage:(NSString *)filename
    {
      NSFileManager *fileManager = ;
      NSString *documentsPath = ;

      NSString *filePath = ;
      NSError *error;
      BOOL success = ;
      if (success) {
          UIAlertView *removedSuccessFullyAlert = [ initWithTitle:@&#34;Congratulations:&#34; message:@&#34;Successfully removed&#34; delegate:self cancelButtonTitle:@&#34;Close&#34; otherButtonTitles:nil];
          ;
      }
      else
      {
          NSLog(@&#34;Could not delete file -:%@ &#34;,);
      }
    }
</code></pre>

<p>其他明智的使用此代码</p>

<pre><code>NSError *error;
if ([ isDeletableFileAtPath:path]) {
    BOOL success = [ removeItemAtPath:path error:&amp;error];
    if (!success) {
      NSLog(@&#34;Error removing file at path: %@&#34;, error.localizedDescription);
    }
}
</code></pre>

<p>你能试试这个代码吗</p>

<pre><code>NSFileManager *fileManager = ;
NSError *error;
BOOL fileExists = ;
NSLog(@&#34;Path to file: %@&#34;, path);      
NSLog(@&#34;File exists: %d&#34;, fileExists);
NSLog(@&#34;Is deletable file at path: %d&#34;, );
if (fileExists)
{
    BOOL success = ;
    if (!success) NSLog(@&#34;Error: %@&#34;, );
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 删除文件名中带有空格的文件 ios,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/37695584/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/37695584/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 删除文件名中带有空格的文件 ios