问题
I have a few data files like
AAA.plist
BBB.plist
CCC.plist
I would like to get the list of these plist file. If I know the file name in advance, I can get the file like this.
let path : String = Bundle.main.path(forResource: "AAA", ofType: "plist")!
var qplist = NSArray(contentsOfFile: path)
However I want to get the plist file list in case of not knowing the file name in advance.
回答1:
You can use urls(forResourcesWithExtension:subdirectory:) for that.
if let urls = Bundle.main.urls(forResourcesWithExtension: "plist", subdirectory: nil) {
print(urls)
}
//Set subdirectory with specific name instead of nil if files inside some directory.
You can also use paths(forResourcesOfType:inDirectory:) to get array of paths.
回答2:
Use the enumerator from FileManager:
let path = Bundle.main.resourcePath
let man = FileManager.default
for file in man.enumerator(atPath: path!)! {
print(file)
}
来源:https://stackoverflow.com/questions/44126087/how-to-get-the-plist-file-lists-in-project