whitebear
whitebear

Reputation: 12445

How to get the plist file lists in project

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.

Upvotes: 0

Views: 496

Answers (2)

David Berry
David Berry

Reputation: 41236

Use the enumerator from FileManager:

let path = Bundle.main.resourcePath
let man = FileManager.default

for file in man.enumerator(atPath: path!)! {
    print(file)
}

Upvotes: 0

Nirav D
Nirav D

Reputation: 72440

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.

Upvotes: 2

Related Questions