在String中获取某个位置的Character,各Swift版本的差异 Swift 3.0 let text = "abc" let index2 = text.index(text.startIndex, offsetBy: 2) //will call succ 2 times let lastChar: Character = text[index2] //now we can index! let characterIndex2 = text.characters.index
Swift的String竟然没有length属性,好难受,每次要获取String的字符串长度都要借助全局函数countElements. 没办法,只有扩展String结构体,给它添加一个属性了. import Foundation extension String { // readonly computed property var length: Int { return countElements(self) } } let a = "hechengmen" println(a.