I'm trying to figure out how to store text information in a MacOS app, in the bundle. But when I try to run the application it can't find the file.
Here's what I'm doing:
Create a brand new Xcode project using the command line app template, called
BundleExperiment
in the directoryrandom_code_projects
From the command line added a text file in the root directory of the created repository (
~\random_code_projects\BundleExperiment
) called "hello.txt" containing "hello cruel world" in what I assume is either ascii or utf-8. (echo "hello cruel world" > hello.txt
)
So the contents of my top-level BundleExperiment
are:
BundleExperiment
|
|- - main.swift
BundleExperiment.xcodeproj
|
|- - a bunch of Xcode generated stuff like project.pbxproj
hello.txt
In xcode 10.2.1, on macOS 10.14.4, in the project navigator, highlighted the "BundleExperiment" folder then used the menu commands
File --> Add Files to "BundleExperiment"
, navigated up tohello.txt
and added it, with the following options selected:- copy items if needed
- create folder references (NOT create groups)
- add to targets (with BundleExperiment checked)
Then the contents of my main.swift file are as follows:
import Foundation
let urlPath = Bundle.main.url(forResource: "hello", withExtension: "txt")!
let hello = try! String(contentsOf: urlPath)
print(hello)
- I build and run by clicking the little play symbol.
And it throws:
Fatal error: Unexpectedly found nil while unwrapping an Optional value
2019-05-05 22:38:23.456979-0500 BundleExperiment[5030:6734193] Fatal error: Unexpectedly found nil while unwrapping an Optional value
What am I doing wrong? I assume that I haven't yet gotten the thing into the bundle, like, there's some random sub-sub-sub-sub menu item with some totally meaningless name like "finagle the squirtle" that means "no, I really wanted to put the thing in the bundle".