Avatar ·

How to replace characters in a string in Swift?

Hello everyone! I recently started learning Swift and encountered a problem when working with strings. I need to replace all spaces in a string with hyphens, and also replace some characters, for example, replace all letters "a" with "o". In other languages, like Python or JavaScript, I'm used to using simple methods like replace(), but in Swift it turned out to be more complicated. Here's what I tried:
var text = "Hello, world! How are you?"
text.replace(" ", "-") // Error!
text.replaceOccurrences(of: " ", with: "-") // Also doesn't work
I get compilation errors. It seems that Swift doesn't have a simple replace method for strings. I googled it, but found many different options: some suggest using NSString, some suggest map, some suggest replacingOccurrences. I'm confused! Please advise,