Home > Enterprise >  How can I iterate inside each directory in resource path
How can I iterate inside each directory in resource path

Time:01-15

I have a simple scenario, there is a folder called content that contains image file, I want all the image file starting with nssl to be saved to an array , so I do below code, but I cannot seem to think or know a way to find out how I can move in each directory and search for such a file and append to my array , here is my code below , I can get the names of all the directories, but what to do next ?

 let path = Bundle.main.resourcePath!
        let fm = FileManager.default
        do {
            let items = try fm.contentsOfDirectory(atPath: path)
            for item in items {
                
               
            }
           
            
        } catch {
            
        }

CodePudding user response:

FileManager is not needed.

Bundle provides urls(forResourcesWithExtension: subdirectory:) which returns multiple urls for a specific extension

if let urls = Bundle.main.urls(forResourcesWithExtension: "png", subdirectory: "content") {
   for url in urls where url.lastPathComponent.hasPrefix("nssl") {            
           
   }
}
       

Change the png extension to the desired type.

  • Related