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

Xcode Project Navigator display doesn't match return value of contentsOfDirectory(atPath path: String) method

Forums > 100 Days of Swift

On Day 16 and noticed something interesting. The Xcode Project Navigator panel shows this:

After running the following code:

let fm = FileManager.default
let path = Bundle.main.resourcePath!
let items = try! fm.contentsOfDirectory(atPath: path)

The constant items will get the following array:

["CodeSignature", "Project-01", "nssl0049.jpg", "Base.lproj", "nssl0046.jpg", "nssl0091.jpg", "nssl0045.jpg", "nssl0051.jpg", "nssl0041.jpg", "nssl0042.jpg", "nssl0043.jpg", "nssl0033.jpg", "Info.plist", "PkgInfo", "nssl0034.jpg"]

That doesn't match what you see in the Project Navigator panel. contentsOfDirectory only does a shallow search so will not display contents of any subdirectories. So it seems what's displayed in the Project Navigator panel is only a visual representation of how the actual data is stored in the bundle - even though the panel displays a folder named Contents which contains all the nssl files, no such directory heirarchy actually exists in the bundle.

3      

According to stuff I've heard and read (sorry, I don't have any link, but Paul mentioned this in one or more videos), when the app is compiled, Xcode puts all resources in a single folder. It doesnt matter whether you created separate folders in your project, Xcode will compile them into a single folder, including image files, sound files, plist files (which a read in run time), etc. So, it makes sense that the listing you got is a bit different from the one shown in Xcode.

3      

hi,

@guseulalio is partially right -- obviously, the swift sources don't get copied since they're compiled, and things like the .plist files have to live at the main level of the bundle for the app to run -- but if you have data files (e.g., some .json files you want to use to pre-populate data for your app) and if you put them in a real folder (not one of those yellow, organizational groups, but a real blue folder-reference), those will go into the bundle as a folder.

you'll find many references about this out on the internet. one such might be this one from swiftdevjournal.com.

hope that helps, DMG

4      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.