如何快速替换特定位置的字母

时间:2020-08-31 12:07:48

标签: ios swift

第一次使用编码器。我正在从头开始制作part子手应用,而没有教程,这是HWS面临的挑战。

我使用UIkit创建了UI,并使用UIButtons()制作了自定义键盘

我的情况是

让这个词成为SWIFT

  • 如果单词有5个字符,它将显示为xxxxx(x的5倍)

  • 当我选择W UIButton时,xxxxx变为xWxxx

  • 如果字母重复超过一次。像字母一样,如果我按T,则xxxxxx会变为xxTTxx。

但是我不知道如何在特定索引位置更改字母。

这是我的代码的样子。

@objc func buttonTapped (_ sender: UIButton) {
Guard let buttonTitle = sender.titleLabel?..text else { return} 

If answer.contains(buttonTitle) { //button title is single letter string I get from buttons qwerty...

var answerLabelTemp = answerLabel.text //text is xxxxx answer is correct answer 

let letterPos = answer.firstIndex(of: buttonTitle.first!)

answerLabelTemp = answerLabelTemp!.replacingOccurrences(of: “x”, with: buttonTitle.first!) // getting error here

answerLabel.text = answerLabelTemp 

}

1 个答案:

答案 0 :(得分:1)

替换字符串中的字符的另一种方法是将单词变成<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table id="yourlist" border="1"> <tr> <th>deviceName</th> <th>count</th> </tr> </table>的数组,然后直接替换字母。使用String将该数组变回.joined()

String

[“ e”,“ l”,“ e”,“ p”,“ h”,“ a”,“ n”,“ t”]

// the hidden word is elephant
let word = "elephant"
let wordArray = word.map(String.init)
print(wordArray)

[“ x”,“ x”,“ x”,“ x”,“ x”,“ x”,“ x”,“ x”]

var hidden = Array(repeating: "x", count: word.count)
print(hidden)

exexxxxx