-1

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 {
            
        }
multiverse
  • 425
  • 3
  • 13
  • Does this answer your question? [Iterate through files in a folder and its subfolders using Swift's FileManager](https://stackoverflow.com/questions/25285016/iterate-through-files-in-a-folder-and-its-subfolders-using-swifts-filemanager) – Luuk Jan 14 '23 at 13:39
  • thanks, the answer by vadian was a bit more short and exactly what I wanted, thanks for your help ... – multiverse Jan 14 '23 at 14:07

1 Answers1

1

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.

vadian
  • 274,689
  • 30
  • 353
  • 361