[英]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.