{"id":2029,"date":"2022-02-27T13:52:33","date_gmt":"2022-02-27T13:52:33","guid":{"rendered":"https:\/\/sezeromer.com\/?p=2029"},"modified":"2023-03-04T14:04:54","modified_gmt":"2023-03-04T11:04:54","slug":"swift-singleton","status":"publish","type":"post","link":"https:\/\/sezeromer.com\/en\/swift-singleton\/","title":{"rendered":"Swift Singleton"},"content":{"rendered":"<p>Hello friends, in this article, we will talk about what <strong>Singleton<\/strong> is and how to use it with Swift.<\/p>\r\n<p>This pattern is very commonly used to avoid static variables and functions in general. We can easily access and use the structure we have developed with a Util, Manager or Singleton model that you have developed.<\/p>\r\n<p>In the example we will do for this article, we will create an <strong>Authentication<\/strong> <strong>Util<\/strong>. The functions that will be in this Util are as follows;<\/p>\r\n<p>Login with username and password<br \/>Sign in with FaceID\/TouchID<br \/>Log out<\/p>\r\n<p>There will also be a variable that controls whether the user is logged in or not when the application is opened. We can do page redirection according to this variable.<\/p>\r\n<p>My design for this example is as follows.<\/p>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2277\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/02\/Screen-Shot-2022-02-27-at-16.41.30.png\" alt=\"\" width=\"497\" height=\"929\" srcset=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/02\/Screen-Shot-2022-02-27-at-16.41.30.png 497w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/02\/Screen-Shot-2022-02-27-at-16.41.30-160x300.png 160w\" sizes=\"(max-width: 497px) 100vw, 497px\" \/><\/p>\r\n<p>After creating 3 functions and variables for the Authentication Util, I create a static variable in this Util instead of creating separate instances on each page where we will use these functions. This variable; It is generally defined as <strong>instance<\/strong>, <strong>shared<\/strong> or <strong>default<\/strong>. I manage everything related to Authentication from here.\u00a0<\/p>\r\n<div class=\"wp-block-codemirror-blocks code-block \">\r\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;}\">\/\/\r\n\/\/  AuthenticationUtil.swift\r\n\/\/  singleton\r\n\/\/\r\n\/\/  Created by \u00d6mer Sezer on 27.02.2022.\r\n\/\/\r\n\r\nimport Foundation\r\n\r\nclass AuthenticationUtil {\r\n    \r\n    static let shared = AuthenticationUtil()\r\n    \r\n    var isLoggedIn = false\r\n    \r\n    func loginWithUsername(username: String, password: String) {\r\n        print(\"login successful\")\r\n    }\r\n    \r\n    func loginWithFaceId() {\r\n        print(\"login successful\")\r\n    }\r\n    \r\n    func logout() {\r\n        print(\"logout\")\r\n    }\r\n}<\/pre>\r\n<\/div>\r\n<p>My View Controller, where I call the functions, is as follows.\u00a0<\/p>\r\n<div class=\"wp-block-codemirror-blocks code-block \">\r\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;}\">\/\/\r\n\/\/  LoginViewController.swift\r\n\/\/  singleton\r\n\/\/\r\n\/\/  Created by \u00d6mer Sezer on 27.02.2022.\r\n\/\/\r\n\r\nimport UIKit\r\n\r\nclass LoginViewController: UIViewController {\r\n\r\n    override func viewDidLoad() {\r\n        super.viewDidLoad()\r\n    }\r\n\r\n    @IBAction func onButtonLoginWithUsernameClicked(_ sender: Any) {\r\n        AuthenticationUtil.shared.loginWithUsername(username: \"test username\", password: \"test password\")\r\n    }\r\n    \r\n    @IBAction func onButtonLoginWithFaceIdClicked(_ sender: Any) {\r\n        AuthenticationUtil.shared.loginWithFaceId()\r\n    }\r\n    \r\n    @IBAction func onButtonLogoutClicked(_ sender: Any) {\r\n        AuthenticationUtil.shared.logout()\r\n    }\r\n}<\/pre>\r\n<\/div>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-2278\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/02\/Screen-Shot-2022-02-27-at-16.41.33-1024x551.png\" alt=\"\" width=\"640\" height=\"344\" srcset=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/02\/Screen-Shot-2022-02-27-at-16.41.33-1024x551.png 1024w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/02\/Screen-Shot-2022-02-27-at-16.41.33-300x161.png 300w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/02\/Screen-Shot-2022-02-27-at-16.41.33-768x413.png 768w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/02\/Screen-Shot-2022-02-27-at-16.41.33-1536x826.png 1536w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/02\/Screen-Shot-2022-02-27-at-16.41.33-500x269.png 500w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/02\/Screen-Shot-2022-02-27-at-16.41.33-800x430.png 800w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/02\/Screen-Shot-2022-02-27-at-16.41.33-1280x689.png 1280w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/02\/Screen-Shot-2022-02-27-at-16.41.33-1920x1033.png 1920w, https:\/\/sezeromer.com\/wp-content\/uploads\/2022\/02\/Screen-Shot-2022-02-27-at-16.41.33.png 2032w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\r\n<p>This way you can use it easily. You can click <a href=\"https:\/\/github.com\/omersezer\/swift-projects\">here<\/a> to examine the project in detail, and <a href=\"https:\/\/sezeromer.com\/swift\/\">here<\/a> to read more swift related articles. If you have questions, you can reach us by sending an e-mail or comment. Good work.<\/p>\r\n\r\n<p>&nbsp;<\/p>","protected":false},"excerpt":{"rendered":"<p>Hello friends, in this article, we will talk about what Singleton is and how to use it with Swift. This pattern is very commonly used to avoid static variables and functions in general. We can easily access and use the structure we have developed with a Util, Manager or Singleton model that you have developed. [&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,706],"tags":[406,890,674,528,878,328,707,889],"class_list":["post-2029","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swift","category-swiftui","tag-default","tag-instance","tag-manager","tag-shared","tag-singleton","tag-swift","tag-swiftui","tag-util"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/2029"}],"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=2029"}],"version-history":[{"count":6,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/2029\/revisions"}],"predecessor-version":[{"id":2687,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/2029\/revisions\/2687"}],"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=2029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/categories?post=2029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/tags?post=2029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}