Reputation: 41
How can i do the same thing in swift 2 :
String a = "danial";
for (int i= 0 ; i < a.length() ; i++) {
System.out.println(a.charAt(i));
}
}
Upvotes: 1
Views: 401
Reputation: 69
Let a = array("danial") // a = ["d", "a", "n", "i", "a", "l"]
i.e. array of Character
Upvotes: -1
Reputation: 1249
Try this:
It was initial created.
let string : String = "danial"
let characters = Array(string)
println(characters)
//["d","a","n","i","a","l"]
String conforms to the SequenceType
protocol, and its sequence generator enumerates the characters.
Swift 2, String does no longer conform to SequenceType
, but the characters property provides a sequence of the Unicode characters:
let string = "danial"
let characters = Array(string.characters)
print(characters)
//["d","a","n","i","a","l"]
Upvotes: 0
Reputation: 3245
Try this :
let myString = "danial"
let characters = Array(myString.characters)
print(characters) //OP : ["d","a","n","i","a","l"]
print(characters[0]) //OP : ["d"]
Upvotes: 2