[英]How to get path to project-file inside iOS-project?
If I add a CSV file into my iOS-project. 如果我将CSV文件添加到我的iOS项目中。 How can I reference that file as the path, for a CSVReader to find the file? 如何将该文件作为路径引用,以便CSVReader查找文件?
At the moment I just drag the file into the Project-Folder in Xcode. 目前我只是将文件拖到Xcode中的Project-Folder中。 The file is there, but then I need to tell the InputStream(fileAtPath: "/path/to/file.csv")! 文件在那里,但我需要告诉InputStream(fileAtPath:“/ path / to / file.csv”)!
How can I tell the InputStream where that path is? 如何告诉InputStream该路径在哪里? Earlier for the simulator I just used the file-path in computer. 早些时候,模拟器我只是在计算机中使用文件路径。 But that won't work when I try on a physical device of course. 但是,当我尝试使用物理设备时,这不起作用。
Any suggestions? 有什么建议么?
Code: 码:
// Get file
let stream = InputStream(fileAtPath: "file.csv")! // Error since no idea where to look for file!
let csv = try! CSVReader(stream: stream)
while let row = csv.next(){
// Handle results
}
If the file is in the application bundle it's located in the Resources
folder. 如果文件位于应用程序包中,则它位于Resources
文件夹中。
You have to ask Bundle
for its URL (file paths are discouraged nowadays). 您必须向Bundle
询问其URL(现在不建议使用文件路径)。
let fileURL = Bundle.main.url(forResource: "file", withExtension: "csv")!
let stream = InputStream(url: fileURL)!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.