gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 07:16:52 29 4
gpt4 key购买 nike

我在 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com