Back to questions
JuniorTheoryCommon
What happens when this program is compiled and run?
let word = "Swift"
let first = word[0]
print(first)It does not compile
- Expecting
S, ass[0]gives in many languages. Characters in Swift take different numbers of bytes, so a string can't be indexed with anInt, andword[0]does not compile. - Expecting a crash at run time, as with an index past the end. Here the compiler already rejects indexing with an
Int, so the program never starts.
- How do you get the first character of
wordwithoutword[0]? - How do you get the third character of a string through
String.Index?