简体   繁体   English

什么是 String.Encoding.unicode?

[英]What is String.Encoding.unicode?

Swift offers a series of encodings for strings. Swift 为字符串提供了一系列编码。 As of the time I'm writing this, none of them are documented , which makes this absurdly more confusing than it should be...截至我写这篇文章的时候,它们都没有被记录在案,这使得这比它应该的更令人困惑......

I can understand that .ascii means it's ASCII encoded, .utf8 means the string is UTF-8 encoded, and .utf16BigEndian means the string is UTF-16 but big-endian .我可以理解.ascii表示它是ASCII编码的, .utf8表示字符串是UTF-8编码的, .utf16BigEndian表示字符串是UTF-16big-endian These obviously map to real text encodings.这些显然是 map 到真正的文本编码。

Then there's .unicode .然后是.unicode There is no "Unicode" encoding.没有“Unicode”编码。 The Unicode standard defines UTF-8, UTF-16, and UTF-32 , which, as I said above, are already defined in Swift. Unicode 标准定义了 UTF-8、UTF-16 和 UTF-32 ,正如我上面所说,它们已经在 Swift 中定义。

Is it a fancy one which figures out the best one for the system?它是一种可以为系统找出最佳方案的花哨吗? Is it an alias for .utf8 ?它是.utf8的别名吗? Is it some weird Apple Unicode encoding?是不是有些奇怪的 Apple Unicode 编码?

It would appear to be an alias for .utf16 .它似乎是.utf16的别名。 From CFString.h :CFString.h

#define kCFStringEncodingInvalidId (0xffffffffU)
typedef CF_ENUM(CFStringEncoding, CFStringBuiltInEncodings) {
    kCFStringEncodingMacRoman = 0,
    kCFStringEncodingWindowsLatin1 = 0x0500, /* ANSI codepage 1252 */
    kCFStringEncodingISOLatin1 = 0x0201, /* ISO 8859-1 */
    kCFStringEncodingNextStepLatin = 0x0B01, /* NextStep encoding*/
    kCFStringEncodingASCII = 0x0600, /* 0..127 (in creating CFString, values greater than 0x7F are treated as corresponding Unicode value) */
    kCFStringEncodingUnicode = 0x0100, /* kTextEncodingUnicodeDefault  + kTextEncodingDefaultFormat (aka kUnicode16BitFormat) */
    kCFStringEncodingUTF8 = 0x08000100, /* kTextEncodingUnicodeDefault + kUnicodeUTF8Format */
    kCFStringEncodingNonLossyASCII = 0x0BFF, /* 7bit Unicode variants used by Cocoa & Java */

    kCFStringEncodingUTF16 = 0x0100, /* kTextEncodingUnicodeDefault + kUnicodeUTF16Format (alias of kCFStringEncodingUnicode) */
    kCFStringEncodingUTF16BE = 0x10000100, /* kTextEncodingUnicodeDefault + kUnicodeUTF16BEFormat */
    kCFStringEncodingUTF16LE = 0x14000100, /* kTextEncodingUnicodeDefault + kUnicodeUTF16LEFormat */

    kCFStringEncodingUTF32 = 0x0c000100, /* kTextEncodingUnicodeDefault + kUnicodeUTF32Format */
    kCFStringEncodingUTF32BE = 0x18000100, /* kTextEncodingUnicodeDefault + kUnicodeUTF32BEFormat */
    kCFStringEncodingUTF32LE = 0x1c000100 /* kTextEncodingUnicodeDefault + kUnicodeUTF32LEFormat */
};

You can confirm this with:您可以通过以下方式确认:

print(String.Encoding.unicode.rawValue, String.Encoding.utf16.rawValue)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM