我如何在swift 2中做同样的事情:
String a = "danial";
for (int i= 0 ; i < a.length() ; i++) {
System.out.println(a.charAt(i));
}
}
答案 0 :(得分:1)
试试这个:
let myString = "danial"
let characters = Array(myString.characters)
print(characters) //OP : ["d","a","n","i","a","l"]
print(characters[0]) //OP : ["d"]
答案 1 :(得分:0)
试试这个:
它是最初创建的。
let string : String = "danial"
let characters = Array(string)
println(characters)
//["d","a","n","i","a","l"]
String符合SequenceType
协议,其序列生成器枚举字符。
Swift 2 ,String不再符合SequenceType
,但characters属性提供了一系列Unicode字符:
let string = "danial"
let characters = Array(string.characters)
print(characters)
//["d","a","n","i","a","l"]
答案 2 :(得分:0)
从Swift 4. *开始,
let stringArray = Array(wordString)
答案 3 :(得分:-1)
Let a = array("danial") // a = ["d", "a", "n", "i", "a", "l"]
即。字符数组