【问题标题】:PDF file not getting deleted in iOSPDF文件在iOS中没有被删除
【发布时间】:2013-06-02 10:59:31
【问题描述】:

我正在通过以下链接创建 PDF 页面...

http://mobile.tutsplus.com/tutorials/iphone/generating-pdf-documents/?search_index=3

我无法使用以下方法删除此 PDF 文件。我已经注释了错误行...

self.fileMgr = [NSFileManager defaultManager];

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];

 NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.pdf",[self.tablePdfListArray objectAtIndex:indexPath.row]]];

 if([self.fileMgr fileExistsAtPath:pdfPath] == YES)
 {

 [fileMgr removeFileAtPath: pdfPath error:nil];  //No visible @interface for NSFilemanager declares the selector removeFileAtPath 
 }

你能推荐一下吗?提前致谢。

【问题讨论】:

    标签: ios6 nsfilemanager nsdocumentdirectory


    【解决方案1】:

    我在 NSFileManager 的类引用中的任何地方都找不到 removeFileAtPath:error: 方法。这似乎是一个非常古老的实例方法。有一个类似的方法 removeFileAtPath:handler: 似乎已被弃用。

    尝试改用 removeItemAtPath:error:。来自class reference of NSFileManager

    removeItemAtPath:error:

    删除指定路径的文件或目录。

    您应该熟悉用法。我建议您将错误参数分配给 NSError 变量,以便您可以在操作结束时检查 NSError,以防万一:

    NSError *error = nil;
    BOOL deleted = [[NSFileManager defaultManager] removeItemAtPath:pdfPath error:&error];
    if (!deleted) {
        NSLog(@"Unable to delete pdf at %@, reason: %@", path, error);
    }
    

    【讨论】:

    • 在插入 removeFileAtPath 之前,我应用了与您建议的方法相同的方法。那个时候以某种方式 removeItemAtPath 不起作用。我想知道它今天是如何工作的。谢谢你的回答:)
    • NSLog 给了我这个 Error Domain=NSCocoaErrorDomain Code=4 “操作无法完成。(Cocoa 错误 4。)” UserInfo=0x1f504530 {NSUnderlyingError=0x1f52efc0 “操作无法完成已完成。没有这样的文件或目录”,NSFilePath=/var/mobile/Applications/8A4C9907-541C-4186-AE79-92772BEBCB16/Documents/so.pdf,NSUserStringVariant=(删除)}。该文件正在被删除。
    • @NavnathMemane 所以文件已被删除但仍有错误消息?
    • 是的。通过删除 tableview 行数组,pdf 文件被删除。而且我无法通过在 tableView 数组中创建相同的名称(以前删除)来访问 PDF 文件。真的要卸了吗?如何交叉检查?
    • @NavnathMemane 您需要检查您的模拟器路径,如上面的错误消息所述。
    猜你喜欢
    • 2012-04-11
    • 1970-01-01
    • 2016-03-28
    • 1970-01-01
    • 2014-06-20
    • 2017-09-30
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多