题目:字符串反转,Swift中字符串反转本身就提供的有函数,不过自己实现也非常正常.
核心代码:
func reverseString(str:String) -> String {
var arr = Array(str.characters)
let count:Int = arr.count
for i in 0..<count / 2 {
swap(&arr[i], &arr[count - i - 1])
}
return String(arr)
}
测试代码:
var reverseTemp:String = "FlyElephant"
var reverseStr:String = myString.reverseString(str: reverseTemp)
print("反转字符串:\(reverseStr)---\(String(reverseTemp.characters.reversed()))")