{"id":969,"date":"2015-09-05T18:37:56","date_gmt":"2015-09-05T22:37:56","guid":{"rendered":"http:\/\/www.peteonsoftware.com\/?p=969"},"modified":"2024-03-01T17:25:35","modified_gmt":"2024-03-01T22:25:35","slug":"swift-extension-methods","status":"publish","type":"post","link":"https:\/\/www.peteonsoftware.com\/index.php\/2015\/09\/05\/swift-extension-methods\/","title":{"rendered":"Swift Extension Methods"},"content":{"rendered":"<p><em style=\"font-size:smaller;\">I&#8217;m doing this code in the latest version of Xcode that is available as of this writing, Xcode 7 Beta 6.  It does not work with Xcode 6 at all, because some of the features are only part of Swift 2.<\/em><br \/>\n<img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201509\/ExtensionCord.png\" alt=\"Extension Cord\" title=\"Extension Cord\" style=\"float:left;margin:.5em;\" \/>Extension methods are a language feature that allows you to add behavior to a class (&#8220;extend&#8221; that class&#8217; functionality, if you will). <\/p>\n<p>In .Net, extension methods are merely syntactic sugar.  I previously talked about them <a href=\"https:\/\/www.peteonsoftware.com\/index.php\/2008\/02\/19\/c-extension-methods\/\">here<\/a> and <a href=\"https:\/\/www.peteonsoftware.com\/index.php\/2012\/06\/21\/c-extension-methods-on-null-objects\/\">here<\/a>.  I go into more detail in the links, but basically extension methods are implemented as new methods that take the &#8220;extended object&#8221; as a parameter.  You might declare one something like this:<\/p>\n<pre>\r\npublic static class StringExtensions\r\n{\r\n    public static bool StartsWithACapitalLetter(this string input)\r\n    {\r\n        return !string.IsNullOrEmpty(input) && Char.IsUpper(input[0]);\r\n    }\r\n}\r\n<\/pre>\n<p>Then, when I call <em>&#8220;Pete&#8221;.StartsWithACapitalLetter()<\/em> it returns true and <em>&#8220;pete&#8221;.StartsWithACapitalLetter()<\/em> returns false.<\/p>\n<p>So, that&#8217;s .Net.  What about Swift?  In Swift, similar functionality can be achieved this way.<\/p>\n<pre>\r\nextension String {\r\n    var StartsWithACapitalLetter:Bool {\r\n        if (self.isEmpty) { return false }\r\n        let firstCharacter = String(self.characters.first!)\r\n        return firstCharacter == firstCharacter.uppercaseString\r\n    }\r\n}\r\n<\/pre>\n<p>As it stands, if I call <em>&#8220;Pete&#8221;.StartsWithACapitalLetter<\/em> I get true and <em>&#8220;pete&#8221;.StartsWithACapitalLetter<\/em> gives me false.  Let&#8217;s break it down a little more.<\/p>\n<p>The first thing you do is just use the Swift keyword <em>extension<\/em> in front of the name of the class you are extending.  After that, you literally just &#8220;add code&#8221; to the class.  In this case, I added a property (not a method) called StartsWithACapitalLetter that returns a boolean.  Notice that within that method, I can use <em>self<\/em> just as if I had written this code inside of the original class itself.  <\/p>\n<p>That&#8217;s really all that there is to it. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m doing this code in the latest version of Xcode that is available as of this writing, Xcode 7 Beta 6. It does not work with Xcode 6 at all, because some of the features &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[77],"tags":[131],"class_list":["post-969","post","type-post","status-publish","format-standard","hentry","category-swift","tag-swift"],"_links":{"self":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/969","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/comments?post=969"}],"version-history":[{"count":0,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/969\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/media?parent=969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/categories?post=969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/tags?post=969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}