Swift 2.0字符串行为 [英] Swift 2.0 String behavior

查看:90
本文介绍了Swift 2.0字符串行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2.0中的字符串不再符合 CollectionType .现在,字符串中的每个字符都是一个扩展石墨烯簇.

Strings in 2.0 no longer conform to CollectionType. Each character in the String is now an Extended Graphene Cluster.

在没有深入研究集群的情况下,我尝试了一些使用Swift Strings的事情:

Without digging too deep about the Cluster stuff, I tried a few things with Swift Strings:

String现在具有一个characters属性,其中包含我们人类识别为 characters 的内容.字符串中的每个不同字符都被视为一个字符,并且count属性为我们提供了不同字符的数量.

String now has a characters property that contains what we humans recognize as characters. Each distinct character in the string is considered a character, and the count property gives us the number of distinct characters.

我不太了解的是,即使characters计数显示为10,为什么index仍显示占2个索引的表情符号?

What I don't quite understand is, even though the characters count shows 10, why does the index show emojis occupying 2 indexes?

推荐答案

Stringindex与Swift 2.0中的字符数(count)不再相关.它是一个不透明的" struct(定义为CharacterView.Index),仅用于遍历字符串中的字符.因此,即使将其打印为整数,也应将其视为整数或将其用作整数,例如,您可以求和2以从当前整数得到第二个字符.您只可以应用两个方法predecessorsuccessor来获取String中的上一个或后续索引.因此,例如,要从mixedString中索引为idx的字符中获取第二个字符,您可以执行以下操作:

The index of a String is no more related to the number of characters (count) in Swift 2.0. It is an "opaque" struct (defined as CharacterView.Index) used only to iterate through the characters of a string. So even if it is printed as an integer, it should not be considered or used as an integer, to which, for instance, you can sum 2 to get the second character from the current one. What you can do is only to apply the two methods predecessor and successor to get the previous or successive index in the String. So, for instance, to get the second character from that with index idx in mixedString you can do:

mixedString[idx.successor().successor()]

当然,您可以使用更舒适的方式来读取字符串的字符,例如for语句或全局函数indices(_:).

Of course you can use more confortable ways of reading the characters of string, like for instance, the for statement or the global function indices(_:).

请考虑这种方法的主要好处不是将Unicode字符串中的多字节字符作为表情符号来威胁,而是以统一的方式对待 (对我们人类来说!)字符串可以在Unicode中具有多种表示形式,如不同的标量"或字符集.一个示例是café,可以用四个Unicode标量"(unicode字符)或五个Unicode标量来表示.请注意,这与将UTF-8,UTF-16等Unicode表示形式映射为内存字节的方式完全不同.

Consider that the main benefit of this approach is not to the threat multi-bytes characters in Unicode strings, as emoticons, but rather to treat in a uniform way identical (for us humans!) strings that can have multiple representations in Unicode, as different set of "scalars", or characters. An example is café, that can be represented either with four Unicode "scalars" (unicode characters), or with five Unicode scalars. And note that this is a completely different thing from Unicode representations like UTF-8, UTF-16, etc., that are ways of mapping Unicode scalars into memory bytes.

这篇关于Swift 2.0字符串行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆