What is difference between URLWithString and fileURLWithPath of NSURL?

+URLWithString: produces an NSURL that represents the string as given. So the string might be @"http://www.google.com" and the URL represents http://www.google.com.

+fileURLWithPath: takes a path, not a URL, and produces an NSURL that represents the path using a file:// URL. So if you give it /foo/bar/baz the URL would represent file:///foo/bar/baz.

You can of course construct a file URL string manually and pass it to +URLWithString:, but +fileURLWithPath: is simpler to use when you already have a path, as you don’t have to deal with escaping the string and coercing it to a URL format.

Leave a Comment