xcode 参数类型“字符串”不符合预期类型“序列”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41037767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Argument type 'String' does not conform to expect type 'Sequence'
提问by Oliver Redeyoff
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 :
我正在为 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"]
So for example, if the user enters a I would like to get "0" and then "1". To do this I use a counter :
因此,例如,如果用户输入 a I would like to get "0" 然后是 "1"。为此,我使用了一个计数器:
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?
我究竟做错了什么?
回答by dfri
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:
实例的characters
属性String
包含包含在String
. 对于给定的键(例如"a"
),您可以.characters
将相应值("01"
)的重新映射到单字符String
实例以获取String
数组:
if let charsForKeyA = Morse["a"]?.characters.map({ String("a" => ["0", "1"]
"b" => ["1", "0", "0", "0"]
"c" => ["1", "0", "1", "0"]
) }) {
charsForKeyA.forEach { print(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"]
}
) }
} /* 0
1 */
回答by Ahmad F
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:
如果我猜对了,您想根据您的示例获取插入键值的字符数组,输出应如下所示:
##代码##and so on...
等等...
##代码##