2

I'm making a very simple app that uses a UIWebView to display a pdf map that can be zoomed in on, panned, etc.

However, when creating a target url for the pdf, the pathForResource call isn't working right. This is my code:

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:

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.

What do I do?

user2397282
  • 3,798
  • 15
  • 48
  • 94

2 Answers2

4

pathForResource() does not return a NSURL object, but a String?. Declare your variable accordingly and it should work.

var targetURL : String? = NSBundle.mainBundle().pathForResource(filename, ofType: type)

Or, of course, if you would rather - just use URLForResource() instead.

var targetURL : NSURL? = NSBundle.mainBundle().URLForResource(filename, withExtension: type)
Simon
  • 3,667
  • 1
  • 35
  • 49
  • any chance you know how to handle a case where targetURL is returned as nil? Is there a way to add a callback/closure to this so I just wait until something is returned? – newshorts Aug 07 '15 at 18:25
  • I'm not quite sure how to answer that I'm afraid, but [this post](http://stackoverflow.com/a/24026093/222245) does a pretty good job explaining *optional types*. Perhaps it will answer your question. – Simon Aug 10 '15 at 13:20
0

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.

Walter
  • 5,867
  • 2
  • 30
  • 43