ursan526
ursan526

Reputation: 535

Replace character in a long dictionary with swift

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)
    }
}

Upvotes: 0

Views: 135

Answers (0)

Related Questions