Swift: Stupid Reflection Tricks #1

Range wars aren’t limited to the old wild west. I was kicking around some code and put together the following, which is admittedly bad on so many levels.

Lily Ballard saw that, sighed, shook his head, and patiently recommended the following instead:

Lily B points out that String.Index is an index of Characters, not UTF-16 code units, and is presumably using some other internal offset. He adds, accessing values with String.Index is a constant-time operation; character offset (or UTF-16 code-unit offset or whatever) is not constant-time so it’s probably using an offset into whatever backing storage the String is currently using.

Comments are closed.