How to get the plist file lists in project

孤街醉人 提交于 2019-12-12 04:33:30

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!