{"id":11811,"date":"2015-07-24T10:25:44","date_gmt":"2015-07-24T14:25:44","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=11811"},"modified":"2015-07-25T00:08:43","modified_gmt":"2015-07-25T04:08:43","slug":"strings-in-swift-2","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/07\/24\/strings-in-swift-2\/","title":{"rendered":"Strings in Swift 2"},"content":{"rendered":"<p><a href=\"https:\/\/developer.apple.com\/swift\/blog\/?id=30\">Apple<\/a> (<a href=\"https:\/\/twitter.com\/SwiftLang\/status\/624332623083495424\">tweet<\/a>):<\/p>\n<blockquote cite=\"https:\/\/developer.apple.com\/swift\/blog\/?id=30\">\n<p>In Swift 2, the <code>String<\/code> type no longer conforms to the <code>CollectionType<\/code> protocol, where <code>String<\/code> was previously a collection of <code>Character<\/code> values, similar to an array. Now, <code>String<\/code> provides a <code>characters<\/code> property that exposes a character collection view.<\/p>\n<p>[&#8230;]<\/p>\n<p>When you add an element to a collection, you expect that the collection will contain that element. That is, when you append a value to an array, the array then contains that value. The same applies to a dictionary or a set. However, when you append a combining mark character to a string, the contents of the string itself change.<\/p>\n<p>[&#8230;]<\/p>\n<p>However, <code>String<\/code> determines equality based on being <em>canonically equivalent<\/em>. Characters are canonically equivalent if they have the same linguistic meaning and appearance, even if they are composed from different Unicode scalars behind the scenes. [&#8230;] In Swift, strings are considered equal regardless of whether they are constructed from decomposed or precomposed character sequences.<\/p>\n<\/blockquote>\n<p>Update (2015-07-25): <a href=\"http:\/\/wanderingcoder.net\/2015\/07\/24\/string-character-processing\/\">Pierre Lebeaupin<\/a>:<\/p>\n<blockquote cite=\"http:\/\/wanderingcoder.net\/2015\/07\/24\/string-character-processing\/\"><p>If Apple can&rsquo;t get its &ldquo;Characters&rdquo;, UTF-16 scalars, and bytes of a seemingly simple string such as &ldquo;cafe&#769;&rdquo; straight in a blog post designed to show these very views of that string, what hope could you possibly have of getting &ldquo;character&rdquo;-wise text processing right?<\/p><\/blockquote>\n<p>Indeed, Apple has corrected its blog post (silently).<\/p>\n<p><a href=\"http:\/\/wanderingcoder.net\/2015\/07\/24\/string-character-processing\/#comment-4929\">marcelolr<\/a>:<\/p>\n<blockquote cite=\"http:\/\/wanderingcoder.net\/2015\/07\/24\/string-character-processing\/#comment-4929\"><p>Baking in a canonicalization policy into a basic type is &#8230; interesting. I&rsquo;m sure somewhere someone will be unhappy their password no longer hashes to the same value.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Apple (tweet): In Swift 2, the String type no longer conforms to the CollectionType protocol, where String was previously a collection of Character values, similar to an array. Now, String provides a characters property that exposes a character collection view. [&#8230;] When you add an element to a collection, you expect that the collection will [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","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":[31,46,30,71,901,258],"class_list":["post-11811","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-ios","tag-languagedesign","tag-mac","tag-programming","tag-swift-programming-language","tag-unicode"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11811","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=11811"}],"version-history":[{"count":4,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11811\/revisions"}],"predecessor-version":[{"id":11829,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11811\/revisions\/11829"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=11811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=11811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=11811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}