Bundle files in xcode


My notes on bundling and reading files in an XCode project

  1. Create a test.txt file and add the text “testing✌️” to it then put it in a folder named test.bundle
  2. Drag and drop it next to your .app file in xcode (copy)
  3. Swift.print(Bundle.main.resourcePath!+"/temp.bundle/test.txt") Output: /Users/James/Library/Developer/Xcode/DerivedData/GitSyncMac-heiwpdjbtaxzhiclikjotucjguqu/Build/Products/Debug/GitSyncMacApp.app/Contents/Resources/temp.bundle/test.txt
static func content(_ path:String)->String?{
    do {
        let content = try String(contentsOfFile:path, encoding:String.Encoding.utf8) as String//encoding: NSUTF8StringEncoding
        return content
    } catch {
        return nil
    }
}
Swift.print(content(Bundle.main.resourcePath!+"/temp.bundle/test.txt"))//Output: testing✌️

Why use .bundle folders?:

Dragging a folder with resources into xcode will also work but the content will not have hierarchical folder structure once you build. .bundle folders on the other hand will retain the hierarchical folder structure

Note:

  • If you update something in a .bundle the .bundle in your app also get’s updated. It does not get updated other wise.
  • Sometimes you have to add the .bundle file to app target -> buildphases -> copy bundle resources
  • If you need to access a file quickly: var sourceURL = URL(fileURLWithPath: Bundle.main.resourcePath!);sourceURL.appendPathComponent(“archive.zip”) (dra the file into src of xcode)