{"id":19651,"date":"2017-11-27T14:46:12","date_gmt":"2017-11-27T19:46:12","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=19651"},"modified":"2020-04-21T13:54:30","modified_gmt":"2020-04-21T17:54:30","slug":"strings-in-swift-4","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/11\/27\/strings-in-swift-4\/","title":{"rendered":"Strings in Swift 4"},"content":{"rendered":"<p><a href=\"https:\/\/oleb.net\/blog\/2017\/11\/swift-4-strings\/\">Ole Begemann<\/a>:<\/p>\n<blockquote cite=\"https:\/\/oleb.net\/blog\/2017\/11\/swift-4-strings\/\">\n<p>Encountering a function that only accepts a <code>Substring<\/code> when you want to pass a <code>String<\/code> is less common &mdash; most functions should either take a <code>String<\/code> or any <code>StringProtocol<\/code>-conforming type. But if you do need to pass a <code>String<\/code>, the quickest way is to subscript the string with the range operator <code>...<\/code> without specifying any bounds[&#8230;]<\/p>\n<p>[&#8230;]<\/p>\n<p>You may be tempted to take full advantage of the existence of <code>StringProtocol<\/code> and convert all your APIs to take <code>StringProtocol<\/code> instances rather than plain <code>String<\/code>s. But the advice of the Swift team is <a href=\"https:\/\/forums.swift.org\/t\/pitch-substring-performance-affordances\/6217\/6\">not to do that<\/a>[&#8230;]<\/p>\n<p>[&#8230;]<\/p>\n<p>Keep in mind, though, that as of Swift 4, <code>StringProtocol<\/code> is not yet intended as a conformance target for your own custom string types. The documentation explicitly warns against it[&#8230;]<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Ole Begemann: Encountering a function that only accepts a Substring when you want to pass a String is less common &mdash; most functions should either take a String or any StringProtocol-conforming type. But if you do need to pass a String, the quickest way is to subscript the string with the range operator ... without [&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":"2020-04-21T17:54:32Z","apple_news_api_id":"cbae263e-d52e-4305-90b9-fa32a61045aa","apple_news_api_modified_at":"2020-04-21T17:54:33Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/Ay64mPtUuQwWQufoyphBFqg","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":[46,71,901,258],"class_list":["post-19651","post","type-post","status-publish","format-standard","hentry","category-programming-category","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\/19651","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=19651"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/19651\/revisions"}],"predecessor-version":[{"id":28726,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/19651\/revisions\/28726"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=19651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=19651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=19651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}