Converting URL to String and back again

后端 未结 10 1336
攒了一身酷
攒了一身酷 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:19

    NOTICE: pay attention to the url, it's optional and it can be nil. You can wrap your url in the quote to convert it to a string. You can test it in the playground.
    Update for Swift 5, Xcode 11:

    import Foundation
    
    let urlString = "http://ifconfig.me"
    // string to url
    let url = URL(string: urlString)
    //url to string
    let string = "\(url)"
    // if you want the path without `file` schema
    // let string = "\(url.path)"
    

提交回复
热议问题