Converting URL to String and back again

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

    Swift 5.

    To convert a String to a URL:

    let stringToURL = URL(string: "your-string")
    

    To convert a URL to a String:

    let urlToString = stringToURL?.absoluteString
    
    0 讨论(0)
  • 2020-12-22 21:22

    Swift 3 (forget about NSURL).

    let fileName = "20-01-2017 22:47"
    let folderString = "file:///var/mobile/someLongPath"
    

    To make a URL out of a string:

    let folder: URL? = Foundation.URL(string: folderString)
    // Optional<URL>
    //  ▿ some : file:///var/mobile/someLongPath
    

    If we want to add the filename. Note, that appendingPathComponent() adds the percent encoding automatically:

    let folderWithFilename: URL? = folder?.appendingPathComponent(fileName)
    // Optional<URL>
    //  ▿ some : file:///var/mobile/someLongPath/20-01-2017%2022:47
    

    When we want to have String but without the root part (pay attention that percent encoding is removed automatically):

    let folderWithFilename: String? = folderWithFilename.path
    // ▿ Optional<String>
    //  - some : "/var/mobile/someLongPath/20-01-2017 22:47"
    

    If we want to keep the root part we do this (but mind the percent encoding - it is not removed):

    let folderWithFilenameAbsoluteString: String? = folderWithFilenameURL.absoluteString
    // ▿ Optional<String>
    //  - some : "file:///var/mobile/someLongPath/20-01-2017%2022:47"
    

    To manually add the percent encoding for a string:

    let folderWithFilenameAndEncoding: String? = folderWithFilename.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
    // ▿ Optional<String>
    //  - some : "/var/mobile/someLongPath/20-01-2017%2022:47"
    

    To remove the percent encoding:

    let folderWithFilenameAbsoluteStringNoEncodig: String? = folderWithFilenameAbsoluteString.removingPercentEncoding
    // ▿ Optional<String>
    //  - some : "file:///var/mobile/someLongPath/20-01-2017 22:47"
    

    The percent-encoding is important because URLs for network requests need them, while URLs to file system won't always work - it depends on the actual method that uses them. The caveat here is that they may be removed or added automatically, so better debug these conversions carefully.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-22 21:31

    In Swift 4 and Swift 3, To convert String to URL:

    URL(string: String)
    

    or,

    URL.init(string: "yourURLString")
    

    And to convert URL to String:

    URL.absoluteString
    

    The one below converts the 'contents' of the url to string

    String(contentsOf: URL)
    
    0 讨论(0)
提交回复
热议问题