Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Cannot convert value of type 'URL' to expected argument type 'String'

I'm having trouble converting URL to string. The getScreenShotDirectory() path is file:///Users/Pat/Desktop/.

My goal is to convert it to String, so the path can look like /Users/Pat/Desktop/

let urlString = getScreenShotDirectory()
let pathURL = URL(string: getScreenShotDirectory())!       // error 

I would gladly provide more code if needed.

like image 399
dbrownjave Avatar asked Jun 19 '17 02:06

dbrownjave


1 Answers

It appears that your getScreenShotDirectory() method is already a URL. So you get the error trying to pass a URL to the URL(string:) method which, of course, expects a String, not a URL.

The simple solution is to properly convert the URL to a path string:

let pathURL = getScreenShotDirectory() // URL
let pathString = pathURL.path // String
like image 83
rmaddy Avatar answered Sep 21 '22 14:09

rmaddy