Argument type 'String' does not conform to expect type 'Sequence'

6k Views Asked by At

I am developing a app for ios and I am trying to get each letter of a member of dictionary which I have defined like this :

var Morse = ["a": "01", "b": "1000", "c": "1010", "d": "100", "e": "0", "f": "0010", "g": "110", "h": "0000", "i": "00", "j": "0111", "k": "101", "l": "0100", "m": "11", "n": "10", "o": "111", "p": "0110", "q": "1101", "r": "010", "s": "000", "t": "1", "u": "001", "v": "0001", "w": "011", "x": "1001", "y": "1011", "z": "1100", "1": "01111", "2": "00111", "3": "00011", "4": "00001", "5": "00000", "6": "10000", "7": "11000", "8": "11100", "9": "11110", "0": "11111", " ": "2"]

So for example, if the user enters a I would like to get "0" and then "1". To do this I use a counter :

var counter = 0
var letter: String = ""
var strings_letter: String = ""
letter = Morse[strings_letter]!
var number = Array(letter)[counter]

But this gives me an issue :

Argument type 'String' does not conform to expect type 'Sequence'

What am I doing wrong?

2

There are 2 best solutions below

0
On BEST ANSWER

The characters property of a String instance contains a sequence of the characters contained in the String. You could, for a given key (say "a") re-map the .characters of the corresponding value ("01") to single-character String instances to obtain a String array:

if let charsForKeyA = Morse["a"]?.characters.map({ String($0) }) {
    charsForKeyA.forEach { print($0) }
} /* 0
     1 */
0
On

If I got it right, you want to get an array of characters for the value of the inserted key, based on your example, the output should be like:

"a" => ["0", "1"]

"b" => ["1", "0", "0", "0"]

"c" => ["1", "0", "1", "0"]

and so on...

var Morse = ["a": "01", "b": "1000", "c": "1010", "d": "100", "e": "0", "f": "0010", "g": "110", "h": "0000", "i": "00", "j": "0111", "k": "101", "l": "0100", "m": "11", "n": "10", "o": "111", "p": "0110", "q": "1101", "r": "010", "s": "000", "t": "1", "u": "001", "v": "0001", "w": "011", "x": "1001", "y": "1011", "z": "1100", "1": "01111", "2": "00111", "3": "00011", "4": "00001", "5": "00000", "6": "10000", "7": "11000", "8": "11100", "9": "11110", "0": "11111", " ": "2"]

let insertedKey = "a"

if let value = Morse[insertedKey] {
    let array = Array(value.characters)

    // here is your array!
    print(array) // ["0", "1"]
}