How to convert this var string to URL in Swift

前端 未结 5 1319
独厮守ぢ
独厮守ぢ 2021-01-30 16:05

I need url filepath be a URL (NSURL in old versions of Swift). I have this:

let paths = NSSearchPathForDirectoriesInDomains(
        .documentDirectory, .userDom         


        
相关标签:
5条回答
  • 2021-01-30 16:10

    In swift 3 use:

    let url = URL(string: "Whatever url you have(eg: https://google.com)")

    0 讨论(0)
  • 2021-01-30 16:14

    In Swift3:
    let fileUrl = Foundation.URL(string: filePath)

    0 讨论(0)
  • 2021-01-30 16:15

    in swift 4 to convert to url use URL

    let fileUrl = URL.init(fileURLWithPath: filePath)
    

    or

    let fileUrl = URL(fileURLWithPath: filePath)
    
    0 讨论(0)
  • 2021-01-30 16:16

    To Convert file path in String to NSURL, observe the following code

    var filePathUrl = NSURL.fileURLWithPath(path)
    
    0 讨论(0)
  • 2021-01-30 16:30

    you need to do:

    let fileUrl = URL(string: filePath)
    

    or

    let fileUrl = URL(fileURLWithPath: filePath)
    

    depending on your needs. See URL docs

    Before Swift 3, URL was called NSURL.

    0 讨论(0)
提交回复
热议问题