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

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

最佳答案

只是:

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

关于swift - 通过添加百分比快速创建字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51419627/

10-12 14:48