NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

How does one iterate over files in a specific folder in the app Bundle?

Forums > Swift

Thanks to the HWS tutorial on iterating over all files in an app Bundle (https://www.hackingwithswift.com/example-code/system/how-to-read-the-contents-of-a-directory-using-filemanager), I’m finally working with FileManager. However, what I can’t figure out is how I can lessen the amount of work on Swift’s part by directing FileManager to a specific folder in my Bundle, and only iterating over files in that folder.

The closest thing I’ve found that seems to have the potential to do this, is FileManager’s enumerator (https://developer.apple.com/documentation/foundation/filemanager/directoryenumerator).

With that, I still can’t direct Swift to a specific folder.

Can someone please point me in the right direction?

   

This is something that I don't fully understand myself yet, so I might be way off here. But from what I have figured out, the "folders" that you create in your project navigator in Xcode are not actually folders. They are called "Groups" instead. But from what I understand, these groups only exist as a way for developers to keep their files more organized, and they basically disappear when the program is compiled. So, once your program is running, I don't think there is a way to search a specific group in the bundle anymore, because as far as the program is concerned, they don't exist.

1      

can you try this, the appendingPathComponent("demo") is the folder you might want to move into ...

let path = Bundle.main.resourceURL!.appendingPathComponent("demo").path

Edit - also reading FlyOstrich response, if this does not not work you can do below as well , this will take care of the groups issue

To make true folders (blue), create them on disk (using, say, the Finder). You can then import them into Xcode using the option "Create folder references for any added folders".

1      

Even if you create folders for the groups, I believe that only creates folders on your computer for those groups. So, you could navigate to your project in Finder, and see the separate folders there. However, when your program compiles, I still don't think it separates the files into folders within the app bundle.

1      

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.