xcode Swift - pathForResource inDirectory 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26060934/
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
Swift - pathForResource inDirectory parameter
提问by user2397282
I'm making a very simple app that uses a UIWebView to display a pdf map that can be zoomed in on, panned, etc.
我正在制作一个非常简单的应用程序,它使用 UIWebView 来显示可以放大、平移等的 pdf 地图。
However, when creating a target url for the pdf, the pathForResource call isn't working right. This is my code:
但是,在为 pdf 创建目标 url 时,pathForResource 调用无法正常工作。这是我的代码:
var targetURL : NSURL = NSBundle.mainBundle().pathForResource(filename, ofType: type)
I get an error on the parentheses before "filename", that says Missing argument for parameter 'inDirectory' in call
. I tried adding an argument for this:
我在“文件名”之前的括号中收到一个错误,即Missing argument for parameter 'inDirectory' in call
. 我尝试为此添加一个论点:
var targetURL : NSURL = NSBundle.mainBundle().pathForResource(filename, ofType: type, inDirectory: "Map")
I don't know what to put for inDirectory
, because I don't know what the directory is - I added the file to my project and it is in the same folder as my ViewController.swift file. Anyway, it doens't really matter, because I get the following error in the same place, Extra argument 'inDirectory in call
.
我不知道该放什么inDirectory
,因为我不知道目录是什么 - 我将该文件添加到我的项目中,它与我的 ViewController.swift 文件位于同一文件夹中。无论如何,这并不重要,因为我在同一个地方收到以下错误,Extra argument 'inDirectory in call
.
What do I do?
我该怎么办?
回答by Simon
pathForResource()
does not return a NSURL
object, but a String?
. Declare your variable accordingly and it should work.
pathForResource()
不返回一个NSURL
对象,而是一个String?
. 相应地声明您的变量,它应该可以工作。
var targetURL : String? = NSBundle.mainBundle().pathForResource(filename, ofType: type)
Or, of course, if you would rather - just use URLForResource()
instead.
或者,当然,如果您愿意 - 只需使用URLForResource()
。
var targetURL : NSURL? = NSBundle.mainBundle().URLForResource(filename, withExtension: type)
回答by Walter
I was having this same problem. In my case it was because my variable (in your case type
) needed to be unwrapped. Adding a bang !
made the error go away and made the code execute.
我遇到了同样的问题。就我而言,这是因为我的变量(在您的情况下type
)需要解包。添加 bang!
使错误消失并使代码执行。