参数类型'String'不符合期望类型'Sequence'

时间:2016-12-08 10:57:27

标签: ios swift xcode variable-types

我正在为ios开发一个应用程序,我正在尝试获取我已定义的字典成员的每个字母:

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"]

因此,例如,如果用户输入我想要“0”然后“1”。为此,我使用了一个计数器:

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

但这给了我一个问题:

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

我做错了什么?

2 个答案:

答案 0 :(得分:4)

characters实例的String属性包含String中包含的一系列字符。对于给定的密钥(比如"a"),您可以将相应值.characters的{​​{1}}重新映射到单字符"01"实例,以获得{String 1}}数组:

String

答案 1 :(得分:3)

如果我做对了,你想获得插入键值的一系列字符,根据你的例子,输出应该是这样的:

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

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

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

依旧......

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"]
}