Daily iOS
Swift

String internals

Why can't you index a String with an integer like text[3], and why is text.count O(n)?

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

A Swift String is made of Unicode grapheme clusters, so one character has a variable byte length, which rules out integer indexing and O(1) count.

Share a question