尽量不要笑或哭 - 我只是在20年后重新开始编码......
我花了4个多小时查看引用并尝试使用代码片段来获取Bundle.main.path来打开我的文本文件所以我可以读取我的应用程序的数据(我的下一步是适当地解析它).
if let filepath = Bundle.main.path(forResource: "newTest", ofType: "txt")
{
do
{
let contents = try String(contentsOfFile: filepath)
print(contents)
}
catch
{
print("Contents could not be loaded.")
}
}
else
{
print("newTest.txt not found.")
}
Run Code Online (Sandbox Code Playgroud)
结果是:"找不到newTest.txt." 无论我如何尝试将文件拖放到项目中,在Xcode中创建文件或使用文件 - >添加文件到...菜单项.