Swift - encode URL

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

If I encode a string like this:

var escapedString = originalString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)

it does

17条回答
  •  失恋的感觉
    2020-11-21 22:49

    SWIFT 4.2

    Sometimes this happened just because there is space in slug OR absence of URL encoding for parameters passing through API URL.

    let myString = self.slugValue
                    let csCopy = CharacterSet(bitmapRepresentation: CharacterSet.urlPathAllowed.bitmapRepresentation)
                    let escapedString = myString!.addingPercentEncoding(withAllowedCharacters: csCopy)!
                    //always "info:hello%20world"
                    print(escapedString)
    

    NOTE : Don't forget to explore about bitmapRepresentation.

提交回复
热议问题