官术网_书友最值得收藏!

Bid bye to string.characters

As part of the changes, this is one of the most welcomed changes. This change eliminates the necessity for a characters array on String, which means now you can reverse them, loop over them character-by-character, map() and flatMap() them, and more than anything, you can now iterate directly over a String object:

let vowels = "AEIOU"
for char in vowels {
print(char)
}

This prints the following:

 A , E , I , O, U

Here, you not only get logical iteration through String, but also specific understanding for collection and sequence:

vowels.count , result is 5, no need of vowels.characters.count
vowels.isEmpty , result is false
vowels.dropFirst() , result is "EIOU"
String(vowels.reversed()) , result is "UOIEA"

There is a small improvement to the way characters behave—now you can obtain the UnicodeScalarView straight from the character, whereas earlier, instantiation of a new String was needed.

主站蜘蛛池模板: 海林市| 南皮县| 维西| 彰化县| 北海市| 北川| 烟台市| 囊谦县| 葫芦岛市| 饶平县| 曲周县| 余干县| 城步| 来凤县| 津南区| 仙桃市| 南溪县| 江川县| 吴桥县| 任丘市| 内丘县| 周口市| 邓州市| 武宁县| 浠水县| 靖江市| 泽州县| 布拖县| 兴仁县| 北流市| 闽清县| 呼玛县| 长岭县| 肇源县| 福海县| 栾川县| 临沭县| 恭城| 驻马店市| 延庆县| 丹巴县|