-3

I am attempting to make a morse code converter in a Swift 3 playground. The function I am using does not successfully initiate the translation process, saying "Type 'String.CharacterView.IndexDistance' (aka 'Int') does not conform to protocol 'Sequence'".

func convertStringToMorse(_ input: String) -> String {
    let stringToConvert = input
    var charsInString = input.characters.count
    var returnString = ""
    for char in charsInString {
        let returnChar = convertLetterToMorse(char)
        if returnChar != "" {
            returnString += returnChar + " "
        }
    }
    return returnString
}

The error happens in the for char in charsInString line, specifically at charsInString. How do I fix this?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Matthew McDonnell
  • 103
  • 1
  • 1
  • 5

1 Answers1

0

Delete the .count.

input.characters.count is an Int representing the number of characters.

input.characters represents the sequence of characters.

You can't use an Int in a for-in loop. You would need a collection or sequence.

Taylor M
  • 1,855
  • 1
  • 14
  • 20