通过添加百分比快速创建字符串

时间:2018-07-19 09:56:33

标签: swift deprecated urlencode deprecation-warning

如何将其转换为当前的Swift语法。我很难过。

extension String {
    var escaped: String {
        return CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,self as CFString!,"[]." as CFString!,":/?&=;+!#$()@',*" as CFString!,CFStringConvertNSStringEncodingToEncoding(String.Encoding.utf8.rawValue)) as String
    }
}

1 个答案:

答案 0 :(得分:0)

简单地:

var escaped: String {
   let allowedCharacters = CharacterSet(charactersIn: ":/?&=;+!#$()@',*")
   return addingPercentEncoding(withAllowedCharacters: allowedCharacters)!
}