{"id":3649,"date":"2019-01-13T03:48:17","date_gmt":"2019-01-13T09:48:17","guid":{"rendered":"https:\/\/dev.iachieved.it\/iachievedit\/?p=3649"},"modified":"2019-01-13T03:48:17","modified_gmt":"2019-01-13T09:48:17","slug":"swift-4-titleized-string-extension","status":"publish","type":"post","link":"https:\/\/dev.iachieved.it\/iachievedit\/swift-4-titleized-string-extension\/","title":{"rendered":"Swift 4 titleized String Extension"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.shields.io\/badge\/Swift-4.2-orange.svg?style=flat\" alt=\"Swift 4.2\" \/><\/p>\n<p>Rails provides the <a href=\"https:\/\/apidock.com\/rails\/ActiveSupport\/Inflector\/titleize\"><i>titleize<\/i><\/a> inflector (capitalizes all the words in a string), and I needed one for Swift too.  The code snippet below adds a <i>computed property<\/i> to the <code>String<\/code> class and follows these rules:<\/p>\n<ul>\n<li>The first letter of the first word of the string is capitalized\n<li>The first letter of the remaining words in the string are capitalized, except those that are considered &#8220;small words&#8221;\n<\/ul>\n<p>Create a file in your Xcode project named <code>String+Titleized.swift<\/code> and paste in the following <code>String<\/code> <a href=\"https:\/\/docs.swift.org\/swift-book\/LanguageGuide\/Extensions.html\">extension<\/a>:<\/p>\n<pre class=\"theme:sublime-text toolbar-overlay:false lang:swift decode:true \" >import Foundation\n\nlet SMALL_WORDS = [\"a\", \"al\", \"y\", \"o\", \"la\", \"el\", \"las\", \"los\", \"de\", \"del\"]\n\nextension String {\n  var titleized:String {\n    var words = self.lowercased().split(separator: \" \").map({ String($0)})\n    words[0] = words[0].capitalized\n    for i in 1..&lt;words.count {\n      if !SMALL_WORDS.contains(words[i]) {\n        words[i] = words[i].capitalized\n      }\n    }\n    return words.joined(separator: \" \")\n  }\n}\n<\/pre>\n<p>Configure <code>SMALL_WORDS<\/code> to your liking.  In this example I was titleizing Spanish phrases, so my <code>SMALL_WORDS<\/code> contains various definite articles and conjunctions.  An example of the usage and output:<\/p>\n<pre class=\"toolbar-overlay:false\">\n 1> \"CORU\u00d1A y SAN ANTONIO\".titleized\n$R0: String = \"Coru\u00f1a y San Antonio\"\n\n 2> \"el d\u00eda de muertos\".titleized\n$R1: String = \"El D\u00eda de Muertos\"\n<\/pre>\n<p><b>Note:<\/b>  This post is a Swift 4.2 version of <a href=\"https:\/\/dev.iachieved.it\/iachievedit\/swift-titleized-string-extension\/\">this one<\/a> written in Swift 2.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rails provides the titleize inflector (capitalizes all the words in a string), and I needed one for Swift too. The code snippet below adds a computed property to the String class and follows these rules: The first letter of the first word of the string is capitalized The first letter of the remaining words in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3108,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[82],"class_list":["post-3649","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swift","tag-swift-string-title-case"],"_links":{"self":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/3649"}],"collection":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/comments?post=3649"}],"version-history":[{"count":3,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/3649\/revisions"}],"predecessor-version":[{"id":3652,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/3649\/revisions\/3652"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media\/3108"}],"wp:attachment":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media?parent=3649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/categories?post=3649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/tags?post=3649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}