Cannot do string[i] on Swift strings to get character at index
| Originator: | |||
| Number: | rdar://17222237 | Date Originated: | 2014-06-08 |
| Status: | Open | Resolved: | |
| Product: | Swift | Product Version: | |
| Classification: | Reproducible: |
Summary:
You'd expect this to work:
var s = "Hello world"
println(s[0])
However, there is no built-in overload for subscript that takes an integer index.
This is a workaround but you'd expect something like this to be in the standard library:
extension String {
subscript(i: Int) -> Character {
return Array(self)[i]
}
}
Steps to Reproduce:
Make a string.
Try to access a character at a specified index.
Expected Results:
Compiler gives an error.
Actual Results:
It returns a Character.
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!