我有这个:
var alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
println("You should know your \(alphabet)'s")
如何访问 a、b、c?
我试过\(alphabet[0...3])的和\(alphabet['0', '1', '2'])的 和 \(alphabet[0,1,2) 无济于事。
我错过了什么?
请您参考如下方法:
您使用 alphabet[0..2] 是正确的,但是您缺少的是 join 函数,该函数使用分隔符连接字符串:
let alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
let section = alphabet[0...2]
let s = join(", ", section) // Separator goes in the ""
println(s) // a, b, c
此外,没有理由使用 var 作为字母表,所以使用 let
