Xcode 7 和 Swift 2.0 的 UITableView 数组中的 DocumentsDirectory
DocumentsDirectory in UITableView Array for Xcode 7 and Swift 2.0
我有一个正在处理的项目,它在本地创建 PDF 文件并将其保存到 DocumentDirectoy
。我已经阅读了 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 的内容,但无法弄清楚如何将 DocumentDirectory
放入 Swift 2.0 中的数组以显示在我的 UITableView
中。我的 PDF 文件正在保存并可以在我的 UIWebView
.
中查看
我查看 DocumentDirectory
PDF 的代码是:
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(){ [=12=].pathExtension == "pdf" }.map{ [=12=].lastPathComponent }
print("PDF FILES:\n" + PDFFiles.description)
} catch let error as NSError {
print(error.localizedDescription)
}
}
它在我 运行 时打印它们。有人可以帮我将 DocumentDirectory
转换为数组以在我的 UITableView
中查看吗?谢谢。
我明白了。在我的主菜单 View Controller
的 ViewDidLoad()
部分,我添加了:
// 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(){ [=10=].pathExtension == "pdf" }.map{ [=10=].lastPathComponent }
// Set TableView array for saved files to DocumentDirectory PDFs
savedPDFFiles = PDFFiles
print(savedPDFFiles)
} catch let error as NSError {
print(error.localizedDescription)
}
在我的另一个 Table View Controller
中,我将 savedPDFFiles
设置为 Global Variable
。加载我的 Table View Controller
时,table 按名称显示我的 DocumentDirectory
文件。
我有一个正在处理的项目,它在本地创建 PDF 文件并将其保存到 DocumentDirectoy
。我已经阅读了 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 的内容,但无法弄清楚如何将 DocumentDirectory
放入 Swift 2.0 中的数组以显示在我的 UITableView
中。我的 PDF 文件正在保存并可以在我的 UIWebView
.
我查看 DocumentDirectory
PDF 的代码是:
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(){ [=12=].pathExtension == "pdf" }.map{ [=12=].lastPathComponent }
print("PDF FILES:\n" + PDFFiles.description)
} catch let error as NSError {
print(error.localizedDescription)
}
}
它在我 运行 时打印它们。有人可以帮我将 DocumentDirectory
转换为数组以在我的 UITableView
中查看吗?谢谢。
我明白了。在我的主菜单 View Controller
的 ViewDidLoad()
部分,我添加了:
// 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(){ [=10=].pathExtension == "pdf" }.map{ [=10=].lastPathComponent }
// Set TableView array for saved files to DocumentDirectory PDFs
savedPDFFiles = PDFFiles
print(savedPDFFiles)
} catch let error as NSError {
print(error.localizedDescription)
}
在我的另一个 Table View Controller
中,我将 savedPDFFiles
设置为 Global Variable
。加载我的 Table View Controller
时,table 按名称显示我的 DocumentDirectory
文件。