Converting URL to String and back again

后端 未结 10 1360
攒了一身酷
攒了一身酷 2020-12-22 20:29

So I have converted an NSURL to a String. So if I println it looks like file:///Users/... etc.

Later I want this

10条回答
  •  渐次进展
    2020-12-22 21:27

    fileURLWithPath() is used to convert a plain file path (e.g. "/path/to/file") to an URL. Your urlString is a full URL string including the scheme, so you should use

    let url = NSURL(string: urlstring)
    

    to convert it back to NSURL. Example:

    let urlstring = "file:///Users/Me/Desktop/Doc.txt"
    let url = NSURL(string: urlstring)
    println("the url = \(url!)")
    // the url = file:///Users/Me/Desktop/Doc.txt
    

提交回复
热议问题