0

Can someone explain this string replacing method(SHORT CODE)

The method is replacing "?" with corespondent letter when its clicked(Hangman game) I cant figure out what offsetBy: location is here for And replacingCharacters(in: index...index if word.contains(letter){ let char = (Character(letter)) var location = 0 for c in word{ if c == char { let index = promptWord.index(promptWord.startIndex, offsetBy: location) promptWord = promptWord.replacingCharacters(in: index...index, with: String(c)) currentWord.text = promptWord } location += 1 } } THIS is just a loadlevel method to show for better understanding if its needed: func loadlevel(){ if let startWordUrl = Bundle.main.url(forResource: "start", withExtension: "txt"){ if let startWord = try? String(contentsOf: startWordUrl){ var allWords = startWord.components(separatedBy: "\n") word = allWords.randomElement()! for letter in word { usedLetter.append(letter) promptWord.append("?") } currentWord.text = promptWord } }

5th Jul 2021, 5:54 PM
Dzondzula
Dzondzula - avatar
1 Answer