Avatar ·

How to get the nth character of a string in Swift? String indexing

Hello everyone! I just started learning Swift and encountered a problem that seems very strange to me. In other programming languages, like Python or JavaScript, I can easily get a character from a string by index by writing something like `str[5]`. But in Swift this doesn't work! Here's my code: ```swift let myString = "Hello, world!" let char = myString[5] // Error: Cannot subscript a value of type 'String' with an argument of type 'Int' ``` I get a compilation error. Why doesn't Swift allow simply accessing a character by index like in other languages? I've heard something about `String.Index`, but I don't understand how it works in practice. I need to write a function that takes a string and an integer n and returns the nth character of that string (starting from 0). How can I do this correctly?