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

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.

主站蜘蛛池模板: 河南省| 许昌县| 申扎县| 辉南县| 乐平市| 贡觉县| 金乡县| 孝义市| 福海县| 华阴市| 同江市| 益阳市| 油尖旺区| 宁海县| 上虞市| 桂平市| 赤峰市| 南开区| 分宜县| 建瓯市| 惠水县| 体育| 巩义市| 松滋市| 阿坝县| 平利县| 信宜市| 云林县| 革吉县| 山阳县| 错那县| 炉霍县| 九江县| 辽中县| 桦川县| 巴楚县| 吴堡县| 莱阳市| 蓬莱市| 连南| 腾冲县|