尽量不要哭笑不得,我才刚回到编程行业20年。
I've spent more than 4 hours looking at references and trying code snippets to get Bundle.main.path to open my text file so I can read in data for my app (my next step is to appropriately parse it).
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.")
}
结果是: “ newTest.txt not found。”无论我如何尝试将文件拖放到项目中,在 Xcode 中创建文件或使用 File-> Add Files to... 菜单项。