Swift - encode URL

后端 未结 17 1875
無奈伤痛
無奈伤痛 2020-11-21 22:20

If I encode a string like this:

var escapedString = originalString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)

it does

17条回答
  •  太阳男子
    2020-11-21 23:09

    Swift 4.2

    A quick one line solution. Replace originalString with the String you want to encode.

    var encodedString = originalString.addingPercentEncoding(withAllowedCharacters: CharacterSet(charactersIn: "!*'();:@&=+$,/?%#[]{} ").inverted)
    

    Online Playground Demo

提交回复
热议问题