简体   繁体   English

获取目录中多个文件的内容

[英]Get the Contents of multiple files in directory

I need to get the contents from multiple plist files and bring them into a single dictionary which is then displayed in a tableView我需要从多个plist文件中获取内容并将它们放入一个dictionary ,然后将其显示在tableView

Using this code I can manually get each path and the contents of the file but I need to be able to do this for all plist files in the directory not just these predefined ones.使用此代码,我可以手动获取每个路径和文件的内容,但我需要能够对目录中的所有 plist 文件执行此操作,而不仅仅是这些预定义的文件。

    func getFiles() {
    let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
    let path = documentDirectory.appending("/MainFolder/File1.plist")
    let path1 = documentDirectory.appending("/MainFolder/File2.plist")
    let path2 = documentDirectory.appending("/MainFolder/File3.plist")
    tableViewData = [NSDictionary(contentsOfFile: path) as! [String : String], NSDictionary(contentsOfFile: path1) as! [String : String], NSDictionary(contentsOfFile: path2) as! [String : String]]
    print(tableViewData)
}

I the display tableViewData in my tableView which gives me each files contents on its own row.我在我的tableView显示tableViewData ,它在自己的行中为我提供每个文件的内容。

I am guessing I probably need an array of file urls filtered by .plist and then some way to get the contents of each file into a [String : String] dictionary.我猜我可能需要一个由.plist过滤的文件 url 数组,然后以某种方式将每个文件的内容放入[String : String]字典中。

I am new to swift, any help or a better way to do this would be great我是 swift 的新手,任何帮助或更好的方法都会很棒

You can read all the plists and iterate over them adding their contents either to 1 array, or to an already kept dictionary.您可以读取所有 plist 并遍历它们,将它们的内容添加到 1 个数组或已保存的字典中。

do {

let directoryContents = try FileManager.default.contentsOfDirectory(at: documentDirectory, includingPropertiesForKeys: nil, options: [])


let allPlistFiles = directoryContents.filter{ $0.pathExtension == "plist" }
var aPlistArray = []
for aPlist in allPlistFiles {
aPlistArray.append(NSDictionary(contentsOfFile: path) as! [String : String])
}

} catch {
    print(error)
}

First of all don't use outdated and objective-c-ish NSSearchPathForDirectoriesInDomains in Swift.首先,不要在 Swift 中使用过时和客观的NSSearchPathForDirectoriesInDomains Use the modern FileManager API.使用现代FileManager API。

Second of all don't use objective-c-ish NSDictionary(contentsOf to read property list data. Use PropertyListSerialization .其次,不要使用objective-c-ish NSDictionary(contentsOf来读取属性列表数据。使用PropertyListSerialization

The function throws that means it hands over all possible errors to the caller.函数throws意味着它将所有可能的错误移交给调用者。 It filters the URLs in the directory by the plist extension and uses the map function to get the dictionary for each URL.它通过plist扩展过滤目录中的 URL,并使用map函数获取每个 URL 的字典。

func getFiles() throws {
    let documentDirectory = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
    let subFolderURL = documentDirectory.appendingPathComponent("MainFolder")
    let allFiles = try FileManager.default.contentsOfDirectory(at: subFolderURL, includingPropertiesForKeys: nil)
    let properListFiles = allFiles.filter{$0.pathExtension == "plist"}
    tableViewData = try properListFiles.compactMap { url -> [String:String]? in
        let data = try Data(contentsOf: url)
        return try PropertyListSerialization.propertyList(from: data, format: nil) as? [String:String]
    }
    print(tableViewData)
}

Be aware that in sandboxed apps the Documents folder is located in the application container.请注意,在沙盒应用程序中, Documents文件夹位于应用程序容器中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 删除文档目录中的多个文件 - Delete multiple files in Document Directory 迅速:从文档目录加载多个文件 - swift: Load multiple files from documents directory 如何检查文件目录中是否存在多个文件? (迅速) - How to check if multiple files exist in documents directory? (Swift) 如何快速从应用程序文档目录中获取所有文件? - How to get all the files from Applications document directory in swift? 无法在真实设备上获取文件内容(swift 文件库应用程序) - Couldn't get files contents on a real device (swift file base app) 无法获取URL的内容 - Cannot get contents of URL 如何使用swift循环遍历目录中的多个(音频/ mp3)文件? - Swift,iOS8 Soundboard AVPlayer - How to loop over multiple (audio/mp3) files in a directory with swift? - Swift, iOS8 Soundboard AVPlayer swift 如何从我们在文档目录中创建的子文件夹中获取所有文件? - swift how to get all files from a sub folder we created in document directory? urlsession,如何获得多个文件下载的整体进度 - urlsession, how would you get the overall progress on multiple files download 如何使用 UTI 获取目录中可由 AVPlayer 播放的视频文件列表? - How can I use UTIs to get a list of video files in a directory that are playable by AVPlayer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM