{"id":9092,"date":"2014-07-06T14:43:13","date_gmt":"2014-07-06T18:43:13","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=9092"},"modified":"2023-08-14T08:55:18","modified_gmt":"2023-08-14T12:55:18","slug":"strings-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2014\/07\/06\/strings-in-swift\/","title":{"rendered":"Strings in Swift"},"content":{"rendered":"<p><a href=\"http:\/\/oleb.net\/blog\/2014\/07\/swift-strings\/\">Ole Begemann<\/a>:<\/p>\n<blockquote cite=\"http:\/\/oleb.net\/blog\/2014\/07\/swift-strings\/\"><p>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 <a href=\"http:\/\/www.objc.io\/issue-9\/unicode.html\">NSString and Unicode<\/a> that I wrote for <a href=\"http:\/\/www.objc.io\/\">objc.io<\/a> 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 <a href=\"https:\/\/developer.apple.com\/library\/prerelease\/ios\/documentation\/Swift\/Conceptual\/Swift_Programming_Language\/StringsAndCharacters.html#\/\/apple_ref\/doc\/uid\/TP40014097-CH7-XID_368\">chapter on Strings and Characters<\/a> in Apple&#700;s Swift book.<\/p>\n<p>[&#8230;]<\/p>\n<p>Swift strings do not have a <code>length<\/code> property. You can use the global <code>countElements()<\/code> function (which works on any <code>Sequence<\/code>, including strings) to count the number of <code>Character<\/code>s in a string. In the following example, <code>countElements()<\/code> counts the number of characters correctly  whereas <code>NSString<\/code> does not. Because of the differences in what constitutes a character between <code>String<\/code> and <code>NSString<\/code>, <code>NSString<\/code>&#700;s <code>length<\/code> property has been renamed <code>utf16count<\/code> when you access it on a Swift string.<\/p>\n<p>[&#8230;]<\/p>\n<p>Because of the way Swift strings are stored, the <code>String<\/code> type does not support random access to its <code>Character<\/code>s via an integer index &mdash; there is no direct equivalent to <code>NSString<\/code>&#700;s <code>characterAtIndex:<\/code> method. Conceptually, a <code>String<\/code> can be seen as a doubly linked list of characters rather than an array.<\/p>\n<p>[&#8230;]<\/p>\n<p>Character and range indices are based on the opaque <code>String.Index<\/code> type, which implements the <code>BidirectionalIndex<\/code> protocol (an extension of the <code>ForwardIndex<\/code> protocol). To construct an index for a specific position, you have to first ask the string for its <code>startIndex<\/code> and then use the global <code>advance()<\/code> function to iterate over all characters between the beginning of the string and the target position (again, an O(N) operation; <code>advance()<\/code> will simply call <code>succ()<\/code> several times):<\/p>\n<p>[&#8230;]<\/p>\n<p>Another implication of this design is that <code>String.Index<\/code> values are not freely interchangeable between strings.<\/p><\/blockquote>\n<p>Update (2014-07-22): <a href=\"http:\/\/oleb.net\/blog\/2014\/07\/swift-strings-updated\/\">Ole Begemann<\/a>:<\/p>\n<blockquote cite=\"http:\/\/oleb.net\/blog\/2014\/07\/swift-strings-updated\/\"><p>I have completely rewritten my article on <a href=\"http:\/\/oleb.net\/blog\/2014\/07\/swift-strings\/\">Strings in Swift<\/a> from earlier this month. Xcode 6 beta 4 fundamentally changed how the <code>String<\/code> and <code>Character<\/code> type handle Unicode characters that are composed of multiple code points.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Ole Begemann: 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2023-08-14T12:55:20Z","apple_news_api_id":"2d12383f-471e-4e2e-a8e5-c47edde26dbb","apple_news_api_modified_at":"2023-08-14T12:55:20Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/ALRI4P0ceTi6o5cR-3eJtuw","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[69,46,71,901,258],"class_list":["post-9092","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-languagedesign","tag-programming","tag-swift-programming-language","tag-unicode"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9092","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=9092"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9092\/revisions"}],"predecessor-version":[{"id":40326,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9092\/revisions\/40326"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=9092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=9092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=9092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}