Xcode-beta (7A152u): Accessing Parts of Swift Strings Using Integers

Originator:john
Number:rdar://21786159 Date Originated:12-Jul-2015 02:45 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode-beta (7A152u)
Classification:Feature (New) Reproducible:Always
 
Summary:
Though abstracting the idea of indices for lookups in collection types has many benefits, it can be an awkward fit for the string class. Looking up a string through a numeric index can be common when doing text processing, especially if you’re just chopping a character or two off the beginning or end of something. Using the String.Index type for this leads to more verbose code. The boilerplate can obscure the intent of the code. It also makes it harder to chain this with another method call, because you need to store the result in an intermediary variable to get its start index.

Steps to Reproduce:
1. Call substringFromIndex on a string using an integer.

Expected Results:
I’d get a substring

Actual Results:
The compiler tells that I need to use the String’s index and I get sad.

Regression:

Notes:
I’ve attached a playground demonstrating the uses of the alternative syntax alongside the existing syntax.

Comments

This is one of the most brilliant design aspects of any string class I have ever seen in the history of computing. It forces you to always handle Unicode strings in a correct manner, ensuring you can't get unpaired surrogates or split grapheme clusters. This design will preemptively solve countless bugs. If you don't know a lot about Unicode, please do a little more reading and I'm sure you'll agree.

By jake.petroules at July 12, 2015, 11:38 p.m. (reply...)

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!