【发布时间】:2017-04-05 08:33:08
【问题描述】:
我有一个数组,其中包含一组三个字母的单词,这将是我正在开发的游戏的基础。我试图在我的 UILabel 中用下划线替换所有这些单词的第二个字符。用户必须敲击正确的元音才能完成单词。
let games: [Game] = {
let firstGame = Game(question: "1", answer: "BAT", choice_1: "A", choice_2: "O", choice_3: "U", choice_4: "E", image: "_bat", audio: "bat.wav")
let secondGame = Game(question: "2", answer: "BIN", choice_1: "A", choice_2: "O", choice_3: "U", choice_4: "E", image: "_bin", audio: "bin.wav")
let thirdGame = Game(question: "3", answer: "BOX", choice_1: "A", choice_2: "O", choice_3: "U", choice_4: "E", image: "_box", audio: "box.wav")
return [firstGame, secondGame, thirdGame]
}()
我找到了 replacingOccurrences 功能,但这个功能只替换了一个字母或一组。用_ 字符替换我的所有元音的最简单方法是什么。
var game: Game? {
didSet {
questionLabel.text = "Question \(String(describing: game!.question)) of 10"
if (game?.image) != nil {
imageView.image = UIImage(named: (game?.image)!)
}
if let answerContent = game?.answer {
answerField.text = answerContent.replacingOccurrences(of: "A", with: "_")
answerField.addTextSpacing()
}
}
}
【问题讨论】:
-
循环查找所有元音?