{"id":1752,"date":"2020-07-13T18:43:18","date_gmt":"2020-07-13T18:43:18","guid":{"rendered":"https:\/\/sezeromer.com\/?p=1752"},"modified":"2023-02-27T22:40:54","modified_gmt":"2023-02-27T19:40:54","slug":"swift-extension","status":"publish","type":"post","link":"https:\/\/sezeromer.com\/en\/swift-extension\/","title":{"rendered":"Swift Extension"},"content":{"rendered":"<p>Hello friends. In this article, we will talk about how to use an extension with Swift. First, let&#8217;s talk about what is Extension. Extension is actually adding a function or another property to a class or struct or even any object. In this article, I will create an extra function for the String class to explain with an example. With this function I can simply add my own name. You can easily use it in many different areas. Instead of writing separate functions for each class and using them, you can just write an extension and use it everywhere. Afterwards, we will be able to do it more comfortably while refactoring.<\/p>\n<p>I created a playground and made my example here. First, we create a structure called Extension and we need to specify which class or structure this extension will affect. I will add a new function to the String class. Then we add our function and this function returns a string value back. In our function, we can take the current value with self, then I add my own name to this existing value.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">import UIKit\r\n\r\nextension String {\r\n    func addMyName() -&gt; String {\r\n        return self + \"Omer\"\r\n    }\r\n}\r\nvar str = \"Hello, playground\"\r\n\r\nstr.addMyName()<\/pre>\n<\/div>\n<p>In fact, adding extensions is that simple and convenient. The result is as follows.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-1764\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/06\/Screen-Shot-2020-06-13-at-00.43.22-1024x614.png\" sizes=\"(max-width: 640px) 100vw, 640px\" srcset=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/06\/Screen-Shot-2020-06-13-at-00.43.22-1024x614.png 1024w, https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/06\/Screen-Shot-2020-06-13-at-00.43.22-300x180.png 300w, https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/06\/Screen-Shot-2020-06-13-at-00.43.22-768x461.png 768w, https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/06\/Screen-Shot-2020-06-13-at-00.43.22-1536x921.png 1536w, https:\/\/sezeromer.com\/wp-content\/uploads\/2020\/06\/Screen-Shot-2020-06-13-at-00.43.22-2048x1229.png 2048w\" alt=\"\" width=\"640\" height=\"384\" \/><\/p>\n<p>If you have questions, you can reach by e-mail or comment. Good work.<\/p>","protected":false},"excerpt":{"rendered":"<p>Hello friends. In this article, we will talk about how to use an extension with Swift. First, let&#8217;s talk about what is Extension. Extension is actually adding a function or another property to a class or struct or even any object. In this article, I will create an extra function for the String class to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1706,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[327],"tags":[702,678,701,340,115,338,328],"class_list":["post-1752","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swift","tag-eklemek","tag-eklenti","tag-extenison","tag-fonksiyon","tag-ios","tag-string","tag-swift"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/1752"}],"collection":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/comments?post=1752"}],"version-history":[{"count":4,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/1752\/revisions"}],"predecessor-version":[{"id":2647,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/1752\/revisions\/2647"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/media\/1706"}],"wp:attachment":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/media?parent=1752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/categories?post=1752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/tags?post=1752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}