我試圖利用一個串的每個單詞的第一個字母裏面,我有以下代碼:無法在一成不變的值使用可變成員的封閉
func makeHeadline(string: String) -> String {
let headline = words.map { (word) -> String in
var word = word
let firstCharacter = word.remove(at: word.startIndex)
return "\(String(firstCharacter).uppercased())\(word)"
}.joined(separator: " ")
return headline
}
不過,我得到以下錯誤:
Cannot use mutating member on immutable value: 'word' is a 'let' constant.
我試圖字(var word)
之前添加變種,但我得到的錯誤:
Parameters may not have the 'var' specifier.
我該如何解決這個問題?
http://stackoverflow.com/q/29261218/2303865 –
@LeoDabus,謝謝你,但我想實現這個功能我自己,因爲TDD課程我在追隨。絕對是你的解決方案避免重新發明輪子。 – lmiguelvargasf
我會建議使用https://developer.apple.com/reference/foundation/nsstring/1416774-enumeratesubstringsbyWords選項 –