用于Xcode 7和Swift 2.0的UITableView Array中的DocumentsDirectory [英] DocumentsDirectory in UITableView Array for Xcode 7 and Swift 2.0

查看:138
本文介绍了用于Xcode 7和Swift 2.0的UITableView Array中的DocumentsDirectory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我正在开发的项目,它可以在本地创建并保存PDF文件到 DocumentDirectoy 。我已阅读此处了解如何查看 DocumentDirectory 并将其过滤为 PDF 文件。我想要在 UITableView 中查看 DocumentDirectory 文件。我知道我必须有一个数组。我看到这里,但是无法弄清楚如何将 DocumentDirectory 转换成Swift 2.0中的数组,以显示在我的 UITableView 中。我的PDF文件正在保存,可以在我的 UIWebView 中查看。



我的代码查看我的 DocumentDirectory PDF是:

  func itemsInDirectory(){

/ /我们只需要获取文档文件夹url
let documentUrl = NSFileManager.defaultManager()。URLsForDirectory(.DocumentDirectory,inDomains:.UserDomainMask).first!

//现在可以获取目录内容(包括文件夹)
do {
let directoryContents = try NSFileManager.defaultManager()。contentsOfDirectoryAtURL(documentsUrl,includingPropertiesForKeys:nil,options: NSDirectoryEnumerationOptions())
print(Items:\(directoryContents))

}将let错误作为NSError {
print(error.localizedDescription)
}

//如果要过滤目录内容,可以这样做:
do {
let directoryUrls = try NSFileManager.defaultManager()。contentsOfDirectoryAtURL(documentsUrl,includingPropertiesForKeys: nil,options:NSDirectoryEnumerationOptions())
print(directoryUrls)
let PDFFiles = directoryUrls.filter(){$ 0.pathExtension ==pdf} .map {$ 0.lastPathComponent}
打印(PDF文件:\\\
+ PDFFiles.description)
}将let错误作为NSError {
print(error.localizedDescri p)
}

}

当我打印运行。有人可以帮助我将 DocumentDirectory 转换为数组,以在我的 UITableView 中查看?谢谢。

解决方案

我想出来了。在我添加的 ViewDidLoad()部分的主菜单 View Controller 中:

  //获取文档文件夹url 
let documentsUrl = NSFileManager.defaultManager()。URLsForDirectory(.DocumentDirectory,inDomains:.UserDomainMask).first!

//仅适用于PDF的
do {
let directoryUrls = try NSFileManager.defaultManager()。contentsOfDirectoryAtURL(documentsUrl,includingPropertiesForKeys:nil,options:NSDirectoryEnumerationOptions())
let PDFFiles = directoryUrls.filter(){$ 0.pathExtension ==pdf} .map {$ 0.lastPathComponent}

//将保存的文件的TableView数组设置为DocumentDirectory PDF
savePDFFiles = PDFFiles
print(savedPDFFiles)

}将let错误作为NSError {
print(error.localizedDescription)
}
表视图控制器
中,我设置 savedPDFFiles



作为全局变量。在加载我的表视图控制器时,该表以名称显示我的 DocumentDirectory 文件。


I have a project I'm working on that creates and saves PDF files locally to the DocumentDirectoy. I've read here on how to view the DocumentDirectory and filtering it for it for PDF files. I want to have the DocumentDirectory files be viewable in a UITableView. I know I have to have an array for that. I saw here on doing this, but can't figure out how to get the DocumentDirectory into an array in Swift 2.0 to display in my UITableView. My PDF files are saving and are viewable in my UIWebView.

My code for viewing my DocumentDirectory PDFs is:

    func itemsInDirectory() {

    // We need just to get the documents folder url
    let documentsUrl =  NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!

    // now lets get the directory contents (including folders)
    do {
        let directoryContents = try NSFileManager.defaultManager().contentsOfDirectoryAtURL(documentsUrl, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions())
        print("Items: \(directoryContents)")

    } catch let error as NSError {
        print(error.localizedDescription)
    }

    // if you want to filter the directory contents you can do like this:
    do {
        let directoryUrls = try  NSFileManager.defaultManager().contentsOfDirectoryAtURL(documentsUrl, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions())
        print(directoryUrls)
        let PDFFiles = directoryUrls.filter(){ $0.pathExtension == "pdf" }.map{ $0.lastPathComponent }
        print("PDF FILES:\n" + PDFFiles.description)
    } catch let error as NSError {
        print(error.localizedDescription)
    }

}

It prints them when I run it. Can someone please help me with converting the DocumentDirectory to an array to view in my UITableView? Thank you.

解决方案

I figured it out. In my main menu View Controller in the ViewDidLoad() section I added:

    // Get the documents folder url
    let documentsUrl =  NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!

    // Filter for PDF only
    do {
        let directoryUrls = try  NSFileManager.defaultManager().contentsOfDirectoryAtURL(documentsUrl, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions())
        let PDFFiles = directoryUrls.filter(){ $0.pathExtension == "pdf" }.map{ $0.lastPathComponent }

        // Set TableView array for saved files to DocumentDirectory PDFs
        savedPDFFiles = PDFFiles
        print(savedPDFFiles)

    } catch let error as NSError {
        print(error.localizedDescription)
    }

In my other Table View Controller, I set savedPDFFiles as a Global Variable. When loading my Table View Controller, the table shows my DocumentDirectoryfiles by name.

这篇关于用于Xcode 7和Swift 2.0的UITableView Array中的DocumentsDirectory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆