为什么在Swift的String API中将unicode作为编码包含在内? [英] Why is unicode included as an Encoding in Swift's String API?
问题描述
我阅读了此阅读后,我意识到unicode是将字符映射到整数的代码点的标准.这些整数如何存储在内存中是一个完全不同的概念.这是.utf8
,.utf16
起作用的地方,它定义了我们将这些整数存储在内存中的方式.
After reading it I realized that unicode is a standard of mapping characters to code points which are integers. How these integers are stored in memory is an entirely different concept. This is where .utf8
, .utf16
come into play, defining the way we store these integers in memory.
在Swift String
API中,有一种方法可以为我们提供用于以各种编码表示String
的数据字节:
In the Swift String
API there is a method which gives us the data bytes used to represent the String
in various encodings:
func data(using encoding: String.Encoding, allowLossyConversion: Bool = false) -> Data?
此方法的第一个参数的类型为String.Encoding
.此结构Encoding
的编码声明为:
The first parameter to this method is of Type String.Encoding
. This struct Encoding
has an encoding declared as:
static let unicode: String.Encoding
现在,该方法突然可以使用编码.unicode
Now suddenly the method can give me data representation of the String
using the encoding .unicode
现在,这实际上与我在阅读提到的博客后得出的结论相反.它为我提供了字符串的数据表示形式,甚至以为unicode并没有为我提供如何存储它的详细信息.
Now this is in fact opposite to what I concluded after reading the mentioned blog. Its giving me data representation of a string, even thought unicode does not provide me details of how it can be stored.
有人可以告诉我我在这里想念什么吗?我现在真的很困惑.
Can any one tell me what am I missing here? I am really confused now.
推荐答案
String.Encoding.unicode
与String.Encoding.utf16
print(String.Encoding.unicode)
print(String.Encoding.utf16)
上面的照片:
- Unicode(UTF-16)
- Unicode(UTF-16)
- Unicode (UTF-16)
- Unicode (UTF-16)
这篇关于为什么在Swift的String API中将unicode作为编码包含在内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!