1
lyricsForName
函數不打印任何東西。 shortNameFromName
確實有效,我檢查了第二個功能有什麼問題。lyricsForName函數不打印任何東西
func shortNameFromName(name : String) ->String{
var shortName = ""
let lowercaseNmae = name.lowercased()
let vowelSet = CharacterSet(charactersIn: "aeiou")
if let range = lowercaseNmae.rangeOfCharacter(from: vowelSet, options: String.CompareOptions.caseInsensitive)
{
shortName = name.substring(from: range.lowerBound)
}
return shortName
}
func lyricsForName(lyricsTemplate : String , fullName : String) ->String{
let bananaFanaTemplate = ["<FULL_NAME>, <FULL_NAME>, Bo B<SHORT_NAME",
"Banana Fana Fo F<SHORT_NAME>",
"Me My Mo M<SHORT_NAME>",
"<FULL_NAME>"].joined(separator: "\n")
let shortName = shortNameFromName(name: fullName)
let lyrics = lyricsTemplate
.replacingOccurrences(of: "<FULL_NAME>", with: fullName)
.replacingOccurrences(of: "<SHORT_NAME>", with: shortName)
print(lyricsForName(lyricsTemplate:bananaFanaTemplate , fullName: "vishnu"))
return lyrics
}
第一個功能完美的工作,但我想第二個功能不能正常工作。我應該怎麼做才能使它工作?
你好 - 你的函數應該是遞歸的(你在函數聲明中調用lyricsForName)?如果您打印歌詞,會發生什麼情況? – Sparky
它不應該遞歸 – vishnu
我現在在外面使用該函數它只打印\ n,相同的結果,當我嘗試打印歌詞 – vishnu