本地文件引用 xcode 7 swift 2
local file reference xcode 7 swift 2
我正在尝试从文本文件中读取数据,当我尝试通过指向文件的绝对 url 加载文件到我的桌面时,内容加载
file = "/Users/melwyn.p/Desktop/data.txt"
text = try String(contentsOfFile: file, encoding: NSUTF8StringEncoding)
但是如果我更改文件路径以引用拖到项目文件夹中的 data.txt 文件,则无法找到该文件,出现以下错误
"The file “data.txt” couldn’t be opened because there is no such file."
file = "data.txt"
text = try String(contentsOfFile: file, encoding: NSUTF8StringEncoding)
But if I change the file path to refer to the data.txt file dragged into the project folder
你要的路径是app内置的data.txt文件的路径。你不知道它是什么;你必须问运行时。调用 NSBundle 的 mainBundle
获取应用程序包,调用其 pathForResource:ofType:
获取文件路径。
我正在尝试从文本文件中读取数据,当我尝试通过指向文件的绝对 url 加载文件到我的桌面时,内容加载
file = "/Users/melwyn.p/Desktop/data.txt"
text = try String(contentsOfFile: file, encoding: NSUTF8StringEncoding)
但是如果我更改文件路径以引用拖到项目文件夹中的 data.txt 文件,则无法找到该文件,出现以下错误
"The file “data.txt” couldn’t be opened because there is no such file."
file = "data.txt"
text = try String(contentsOfFile: file, encoding: NSUTF8StringEncoding)
But if I change the file path to refer to the data.txt file dragged into the project folder
你要的路径是app内置的data.txt文件的路径。你不知道它是什么;你必须问运行时。调用 NSBundle 的 mainBundle
获取应用程序包,调用其 pathForResource:ofType:
获取文件路径。