基于Swift4,最后一题字符串处理的题目可以通过字符串索引的方式来做:
func reversedByWord(sentence: String) -> String {
var lastWordIndex = sentence.startIndex
var copySentence = sentence
var ans = ""
copySentence.append(" ")
for i in copySentence.indices {
if copySentence[i] == " " {
let subString = String(copySentence[lastWordIndex..<i].reversed())
lastWordIndex = copySentence.index(after: i)
ans += subString + " "
}
}
ans.removeLast()
return ans
}
reversedByWord(sentence: "The quick brown fox jumps over the lazy dog")
func reversedSantence(sentence: String) -> String {
let ans = String(reversedByWord(sentence: sentence).reversed())
return ans
}
reversedSantence(sentence: "the sky is blue")
上次讲解了基本的语法和一些Swift的小技巧。这期我们来看几个最基本的数据结构:数组,字符串,集合和字典。 数组 数组是最基本的数据结构。Swift中改变了以前Objecti...