swift - FileManager.default.contentsOfDirectory 在 swift 3 中失败

标签 swift swift3

我在 objective -c 中有这样的代码:

- (NSArray *)PDFInDirectory:(NSString *)directoryPath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *dirContents = [fileManager contentsOfDirectoryAtPath:directoryPath
                                                            error:nil];
    return [dirContents filteredArrayUsingPredicate:self.pdfPredicate];
}

我想以这种方式翻译成 swift 3:

func PDFInDirectory(directoryPath: String) -> NSArray {
     let dirContents = try? FileManager.default.contentsOfDirectory(atPath: directoryPath) as NSArray
     return dirContents!.filtered(using: ppdfPredicate()) as NSArray
}

但是当我运行它时失败了:

let dirContents = try? FileManager.default.contentsOfDirectory(atPath: directoryPath) as NSArray

这里是所有代码:

import UIKit

class PdfFilePicker: UITableViewController {
    // Variable 
    var NNOTIFICATION_NAME_SELECTED_PDF = "VSSelectedPDFFile"
    var documentsDirectory = ""
    var inboxDirectory = ""
    var pdfPredicate: NSPredicate?
    var pdfFiles: NSMutableArray = []
    func ddocumentsDirectory() -> String {
        if self.documentsDirectory == "" {
        let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) as NSArray
            self.documentsDirectory = paths.object(at: 0) as! String
        }
        print("Document directory call")
    return documentsDirectory
    }

    func ppdfPredicate() -> NSPredicate {

        if self.pdfPredicate == nil {
            self.pdfPredicate = NSPredicate(format: "self ENDSWITH '.pdf'")
        }
        return pdfPredicate!
    }
    func ppdfFiles() -> NSMutableArray {
        if self.pdfFiles == [] {
            self.pdfFiles = [20]
        }
        return  pdfFiles
    }
    func PDFInDirectory(directoryPath: String) -> NSArray {
        // Here is the probelm.
        print("My dir is \(directoryPath)")
        do {
            let dirContents = try? FileManager.default.contentsOfDirectory(atPath: directoryPath) as NSArray
            print("my dirContents \(dirContents)")
            return dirContents!.filtered(using: ppdfPredicate()) as NSArray
        } catch {
            print("my dirPath \(directoryPath)")
        }
        return []
    }
    func populateAllPdfsFromDocumentsAndInboxDirectory() {
        self.ppdfFiles().removeAllObjects()
        self.populatePDFfilesInFolder(path: self.ddocumentsDirectory())
        let inboxDirectory : String = URL(fileURLWithPath: self.ddocumentsDirectory()).appendingPathComponent("Inbox").absoluteString
        self.populatePDFfilesInFolder(path: inboxDirectory)
        self.sortPdfFilesArray()
    }
    // Finished until here. // Checkif it crash probably yes
    func populatePDFfilesInFolder(path: String) {
        var isDir: ObjCBool = false
        let fileManager = FileManager.default
        let isExist = fileManager.fileExists(atPath: path, isDirectory: &isDir)
        if isExist == false   {
            print("Read file manager successfully")
            let array = self.PDFInDirectory(directoryPath: path)
            print("PDF Steve")
            if array.count > 0 {
                for fileName: String in array as! Array {
                    let test : String = URL(fileURLWithPath: path).appendingPathComponent(fileName).absoluteString
                    self.ppdfFiles().adding(test)
                }
            }
        }
    }
    // Can be crash in closure
    func sortPdfFilesArray() {
        if self.ppdfFiles().count > 0 {
            self.ppdfFiles().sort(comparator: {( a: Any, b: Any) -> ComparisonResult in
                if !(a is String) || !(b is String) {
                  return ((a as! String).compare(b as! String))
                } else {
                    let aString: String? = (a as? String)
                    let bString: String? = (b as? String)
                    return (aString?.compare(bString!, options: .caseInsensitive))!
                }
            })
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = NSLocalizedString("SELECT_PDF", comment: "")
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.populateAllPdfsFromDocumentsAndInboxDirectory()
    }
    // Steve Note: Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.ppdfFiles().count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell  = tableView.dequeueReusableCell(withIdentifier: "Cell")
        if cell == nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
        }
        // steve note Not sure about object
        cell?.imageView?.image = UIImage(named: "Icon-PDF.png")
        cell?.textLabel?.text = (self.ppdfFiles().object(at: indexPath.row) as AnyObject).lastPathComponent
        return cell!
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        NotificationCenter.default.post(name: Notification.Name(rawValue: NNOTIFICATION_NAME_SELECTED_PDF), object: self.ppdfFiles().object(at: indexPath.row))
    }
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            self.deleteFile(atPath: self.ppdfFiles().object(at: indexPath.row) as! String)
            self.ppdfFiles().removeObject(at: indexPath.row)
            self.tableView.reloadData()
        }
    }
    func deleteFile(atPath path: String) {
        let fileManager = FileManager.default
        if fileManager.fileExists(atPath: path, isDirectory: nil) {
            do {
                try fileManager.removeItem(atPath: path)
            } catch {
                print("File Manager Remove Item at Path crash")
            }
        }
    }
}

感谢任何帮助。

最佳答案

更新示例

你需要捕获每次使用'try'时可能产生的异常 这是我在当前项目中使用的示例。我有一些文件存储在一个名为 templates 的文件夹中,这段代码确实返回了一个文件名数组——前提是那里已经有了一些东西

    let dirPaths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    let documentDirectory = dirPaths[0]
    let folder = documentDirectory.appending("/templates")

    do
    {
        let fileList = try FileManager.default.contentsOfDirectory(atPath: folder)
        for file in fileList
        {
            fileListArray.append(file)
        }
        return fileListArray
    }
    catch
    {
        addLogText(error.localizedDescription)
        return []
    }

可能值得检查一下您将文件写入的目录是否与您从中读取文件的目录相同 - 您是否尝试过确认文件已创建然后立即读取它们?

关于swift - FileManager.default.contentsOfDirectory 在 swift 3 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41699168/

相关文章:

swift - 带有 .xib-File 的 @IBDesignable 类未在 Interface Builder 中呈现

iOS 8 灯状态栏不工作

ios - 协议(protocol)要求委托(delegate)继承自 UIViewController

ios - 设置UILabel的背景为透明黑色

ios - 如何将访问 token 传递给 Alamofire?

ios - 状态栏在关闭模态视图后保持隐藏状态并在几秒钟后出现

ios - 订阅属性

ios - 如何使用 Swift 在 iOS 中刷新/重绘/重新加载 UIGraphicsImageRenderer

ios - iOS Swift 3 中来自对象的 JSON 存在数组问题

ios - 关闭 View Controller 不会返回到前一个 View Controller