{"id":1693,"date":"2015-09-19T16:42:35","date_gmt":"2015-09-19T22:42:35","guid":{"rendered":"http:\/\/dev.iachieved.it\/iachievedit\/?p=1693"},"modified":"2015-09-19T16:42:35","modified_gmt":"2015-09-19T22:42:35","slug":"swift-titleized-string-extension","status":"publish","type":"post","link":"https:\/\/dev.iachieved.it\/iachievedit\/swift-titleized-string-extension\/","title":{"rendered":"Swift titleized String Extension"},"content":{"rendered":"<p><b>Editor&#8217;s Note:<\/b>  This code is compatible with <a href=\"https:\/\/developer.apple.com\/swift\/blog\/?id=29\">Swift 2.0<\/a> and has not been tested on previous versions of the language!<\/p>\n<p>Rails provides the <a href=\"http:\/\/apidock.com\/rails\/ActiveSupport\/Inflector\/titleize\"><i>titleize<\/i><\/a> inflector, 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:<\/p>\n<pre>\r\nimport Foundation\r\n\r\nlet SMALL_WORDS = [\"a\", \"al\", \"y\", \"o\", \"la\", \"el\", \"las\", \"los\", \"de\", \"del\"]\r\n\r\nextension String {\r\n  var titleized:String {\r\n    var words = self.lowercaseString.characters.split(\" \").map { String($0) }\r\n    words[0] = words[0].capitalizedString\r\n    for i in 1..<words.count {\r\n      if !SMALL_WORDS.contains(words[i]) {\r\n        words[i] = words[i].capitalizedString\r\n      }\r\n    }\r\n    return words.joinWithSeparator(\" \")\r\n  }\r\n}\r\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>\r\n 1> \"CORU\u00d1A y SAN ANTONIO\".titleized\r\n$R0: String = \"Coru\u00f1a y San Antonio\"\r\n\r\n 2> \"el d\u00eda de muertos\".titleized\r\n$R1: String = \"El D\u00eda de Muertos\"\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Editor&#8217;s Note: This code is compatible with Swift 2.0 and has not been tested on previous versions of the language! Rails provides the titleize inflector, 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1693","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/1693"}],"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=1693"}],"version-history":[{"count":4,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/1693\/revisions"}],"predecessor-version":[{"id":1697,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/1693\/revisions\/1697"}],"wp:attachment":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media?parent=1693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/categories?post=1693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/tags?post=1693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}