Strings in Swift
In this article, I want to take a closer look at how strings are handled in Swift. I see this as a follow-up to a piece titled NSString and Unicode that I wrote for objc.io a while ago. Please refer to that article for a more thorough explanation of the Unicode features I mention below. I also assume that you have read the chapter on Strings and Characters in Appleʼs Swift book.
[…]
Swift strings do not have a
lengthproperty. You can use the globalcountElements()function (which works on anySequence, including strings) to count the number ofCharacters in a string. In the following example,countElements()counts the number of characters correctly whereasNSStringdoes not. Because of the differences in what constitutes a character betweenStringandNSString,NSStringʼslengthproperty has been renamedutf16countwhen you access it on a Swift string.[…]
Because of the way Swift strings are stored, the
Stringtype does not support random access to itsCharacters via an integer index — there is no direct equivalent toNSStringʼscharacterAtIndex:method. Conceptually, aStringcan be seen as a doubly linked list of characters rather than an array.[…]
Character and range indices are based on the opaque
String.Indextype, which implements theBidirectionalIndexprotocol (an extension of theForwardIndexprotocol). To construct an index for a specific position, you have to first ask the string for itsstartIndexand then use the globaladvance()function to iterate over all characters between the beginning of the string and the target position (again, an O(N) operation;advance()will simply callsucc()several times):[…]
Another implication of this design is that
String.Indexvalues are not freely interchangeable between strings.
Update (2014-07-22): Ole Begemann:
I have completely rewritten my article on Strings in Swift from earlier this month. Xcode 6 beta 4 fundamentally changed how the
StringandCharactertype handle Unicode characters that are composed of multiple code points.
1 Comment RSS · Twitter
Strings in Swift seem like the place where the most work is needed. However I suspect we won't see a fleshed out string class before iOS8 is released.