xcode 如何将文件添加到项目并引用它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32665546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add file to a project and reference it?
提问by Fedor Tsyganov
I'm really new to iOS development, so this question might be silly, but I didn't find an answer for it.
我对 iOS 开发真的很陌生,所以这个问题可能很愚蠢,但我没有找到答案。
I have a project in swift for iOS device and I want to include a csv file in my project and then use it in code, how can I add this file to the project and then use it in viewController class?
我有一个适用于 iOS 设备的 swift 项目,我想在我的项目中包含一个 csv 文件,然后在代码中使用它,如何将此文件添加到项目中,然后在 viewController 类中使用它?
this is my project structure (yes, it's very simple)
这是我的项目结构(是的,很简单)
and this is what i'm trying to do in code, but it doesn't work.
这就是我试图在代码中做的事情,但它不起作用。
Thank you :)
谢谢 :)
回答by Midhun MP
You can add a resource file to project by just drag and dropping. When you do so, a popup will appear, you need to select your target in it for linking it with your current target.
您只需拖放即可将资源文件添加到项目中。当您这样做时,将出现一个弹出窗口,您需要在其中选择您的目标以将其与当前目标链接。
In your above code there is a mistake. You only need to specify the filename without extension in pathForResource:
method.
在你上面的代码中有一个错误。您只需要在pathForResource:
方法中指定不带扩展名的文件名。
Instead of:
代替:
let file = "europe_dancing.csv"
You only need to specify:
您只需要指定:
let file = "europe_dancing"