UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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?

2      

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.

3      

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".

3      

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.

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.