0

I want to replace some characters in a dictionarys keys. There are about a hundred thousand keys in the dictionary. With the code below, this takes about an hour. Can we shorten this time by making changes in the coding?

for example:

dict.charactersToChange[x][1] = "á à ã ả ạ â ấ ầ ẫ ẩ ậ ă ắ ằ ẵ ẳ ặ"

dict.charactersToChange[x][0] = "a"
func charactertransformation() {
    for (key, _) in words {
        var newWord = key.lowercased(with: Locale(identifier: dict.localIdentifier))

        for x in 0..<dict.charactersToChange.count {
            dict.charactersToChange[x][1].forEach { char in
                newWord = newWord.replacingOccurrences(of: String(char), with: dict.charactersToChange[x][0])
            }
        }

        words.switchKey(fromKey: key, toKey: newWord)
    }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690
ursan526
  • 485
  • 3
  • 10
  • You'll get more engagement from answerers if you translate your code to English, so they can better understand it. – Alexander Oct 02 '21 at 17:20
  • 2
    This might be an XY problem. Are you just trying to remove the accents and diacritics from a String? There's already a built-in method to do that performantly: https://stackoverflow.com/a/29522097/3141234 – Alexander Oct 02 '21 at 17:22
  • @Alexander Isn't the code is in English? – Kevin M. Mansour Oct 02 '21 at 17:22
  • 3
    @KevinM.Mansour It was edited to be, but it was originally German: https://stackoverflow.com/posts/69418623/revisions – Alexander Oct 02 '21 at 17:35
  • yes i fixed the code. yes, this is what i was looking for. by checking now @Alexander – ursan526 Oct 02 '21 at 17:41

0 Answers0