为什么CFStringEncodings没有Swift的UTF8 ?

时间:2021-04-19 10:04:24

I am trying to create a percent encoded string in Swift so I can safely send text as a GET request. I found some Objective C code which I am trying to convert to Swift. I've written the following Swift code:

我正在尝试在Swift中创建一个百分比编码的字符串,这样我就可以安全地发送文本作为GET请求。我找到了一些目标C代码,我想把它转换成Swift。我写了以下Swift代码:

CFURLCreateStringByAddingPercentEscapes(nil, 
    CFStringRef(encodedString), nil, 
    CFStringRef("/%&=?$#+-~@<>|\\*,.()[]{}^!"), 
    kCFStringEncodingUTF8)

There is no kCFStringEncodingUTF8 in Swift ... If you right click the CFStringEncodings source you see there is a million things in there but no UTF8. I don't understand. How can I use the UTF8 string encoding in this situation?

在Swift中没有kCFStringEncodingUTF8…如果您右键单击CFStringEncodings源文件,您会看到其中有100万种东西,但没有UTF8。我不明白你的意思。在这种情况下,如何使用UTF8字符串编码?

EDIT : I found a way to encode a string but I still don't understand what happened to kCFStringEncodingUTF8

编辑:我找到了一种编码字符串的方法,但是我仍然不明白kCFStringEncodingUTF8发生了什么

2 个解决方案

#1


9  

The UTF-8 string encoding is defined as

UTF-8字符串编码定义为

enum CFStringBuiltInEncodings : CFStringEncoding {
    // ...
    case UTF8 /* kTextEncodingUnicodeDefault + kUnicodeUTF8Format */
    // ...
}

and can be used as

可以用作

let orig = "a/b(c)"
let escaped = CFURLCreateStringByAddingPercentEscapes(nil, orig, nil,
    "/%&=?$#+-~@<>|\\*,.()[]{}^!",
    CFStringBuiltInEncodings.UTF8.rawValue)
println(escaped)
// a%2Fb%28c%29

As mentioned by @Zaph in a comment, stringByAddingPercentEncodingWithAllowedCharacters might be easier to use:

@Zaph在评论中提到,stringByAddingPercentEncodingWithAllowedCharacters可能更容易使用:

let charset = NSCharacterSet(charactersInString: "/%&=?$#+-~@<>|\\*,.()[]{}^!").invertedSet
let escaped = orig.stringByAddingPercentEncodingWithAllowedCharacters(charset)

or use one of the pre-defined character sets from Creating a Character Set for URL Encoding.

或者使用为URL编码创建字符集的预定义字符集。

#2


2  

kCFStringEncodingUTF8 has been replace in Swift with: UTF8.

在Swift中,kCFStringEncodingUTF8已被替换为:UTF8。

There is a new NSString method added in iOS7 that takes a character set and a number of specialized character sets have been added and of course you can create specialized character sets. There is very little reason to use CFURLCreateStringByAddingPercentEscapes any more. See this: SO Answer

iOS7中增加了一个新的NSString方法,该方法接受一个字符集,并添加了许多专门的字符集,当然您可以创建专门的字符集。几乎没有理由再使用cfurlcreatestringbyaddingpercent转义了。看到这个:所以回答

#1


9  

The UTF-8 string encoding is defined as

UTF-8字符串编码定义为

enum CFStringBuiltInEncodings : CFStringEncoding {
    // ...
    case UTF8 /* kTextEncodingUnicodeDefault + kUnicodeUTF8Format */
    // ...
}

and can be used as

可以用作

let orig = "a/b(c)"
let escaped = CFURLCreateStringByAddingPercentEscapes(nil, orig, nil,
    "/%&=?$#+-~@<>|\\*,.()[]{}^!",
    CFStringBuiltInEncodings.UTF8.rawValue)
println(escaped)
// a%2Fb%28c%29

As mentioned by @Zaph in a comment, stringByAddingPercentEncodingWithAllowedCharacters might be easier to use:

@Zaph在评论中提到,stringByAddingPercentEncodingWithAllowedCharacters可能更容易使用:

let charset = NSCharacterSet(charactersInString: "/%&=?$#+-~@<>|\\*,.()[]{}^!").invertedSet
let escaped = orig.stringByAddingPercentEncodingWithAllowedCharacters(charset)

or use one of the pre-defined character sets from Creating a Character Set for URL Encoding.

或者使用为URL编码创建字符集的预定义字符集。

#2


2  

kCFStringEncodingUTF8 has been replace in Swift with: UTF8.

在Swift中,kCFStringEncodingUTF8已被替换为:UTF8。

There is a new NSString method added in iOS7 that takes a character set and a number of specialized character sets have been added and of course you can create specialized character sets. There is very little reason to use CFURLCreateStringByAddingPercentEscapes any more. See this: SO Answer

iOS7中增加了一个新的NSString方法,该方法接受一个字符集,并添加了许多专门的字符集,当然您可以创建专门的字符集。几乎没有理由再使用cfurlcreatestringbyaddingpercent转义了。看到这个:所以回答