0

I wanted to create a morse code translator app on xcode. The translation for English to Morse works fine, but this reverse process does not. Whats wrong with my code?

let input = inputTextField.text!
    var output = ""

    var Dict = [".-" : "a", "-..." : "b"]


    for character in input.characters {
        let characters = "\(character)"

        if let translatedcharacters = Dict[characters] {
            output += translatedcharacters
        } else {
            output += characters
        }
    }
     outputLabel.text = output

the output shown is not "a" or "b" but just the input which is either ".-" or "-..."

Kenneth
  • 1
  • 1
  • Well, the idea of a dictionary is a key/value pair. If you want to reverse it, you have to reverse the dictionary so that the value becomes the key. It's not the dictionary's forte to look up by value - or the correct way to use it. – Callum Linington Feb 07 '17 at 09:23
  • Callum is right as a dictionary ensures you have unique keys but you can have several times the same value. So even reversing it is not safe. – Ocunidee Feb 07 '17 at 09:26

0 Answers0