{"id":13990756,"url":"https://github.com/hpique/SwiftSingleton","last_synced_at":"2025-07-22T13:31:19.294Z","repository":{"id":145219661,"uuid":"20672942","full_name":"hpique/SwiftSingleton","owner":"hpique","description":"An exploration of the Singleton pattern in Swift","archived":false,"fork":false,"pushed_at":"2016-11-05T18:02:48.000Z","size":256,"stargazers_count":1133,"open_issues_count":3,"forks_count":101,"subscribers_count":39,"default_branch":"master","last_synced_at":"2024-11-19T23:58:20.323Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hpique.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2014-06-10T04:57:25.000Z","updated_at":"2024-07-16T13:17:57.000Z","dependencies_parsed_at":null,"dependency_job_id":"b4a3cc18-8442-4019-b75c-87c8dc76be80","html_url":"https://github.com/hpique/SwiftSingleton","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hpique%2FSwiftSingleton","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hpique%2FSwiftSingleton/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hpique%2FSwiftSingleton/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hpique%2FSwiftSingleton/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hpique","download_url":"https://codeload.github.com/hpique/SwiftSingleton/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227066312,"owners_count":17725652,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-09T13:03:11.245Z","updated_at":"2024-11-29T10:31:42.636Z","avatar_url":"https://github.com/hpique.png","language":"Swift","funding_links":[],"categories":["Swift","Swift Courses"],"sub_categories":[],"readme":"SwiftSingleton\n==============\n\n_tl;dr: Use the **class constant** approach if you are using Swift 1.2 or above and the **nested struct** approach if you need to support earlier versions._\n\nAn exploration of the Singleton pattern in Swift. All approaches below support lazy initialization and thread safety.\n\nIssues and pull requests welcome.\n\n### Approach A: Class constant\n\n```swift\nclass SingletonA {\n    \n    static let sharedInstance = SingletonA()\n    \n    init() {\n        println(\"AAA\");\n    }\n    \n}\n```\n\nThis approach supports lazy initialization because Swift lazily initializes class constants (and variables), and is thread safe by the definition of `let`.\n\nClass constants were introduced in Swift 1.2. If you need to support an earlier version of Swift, use the nested struct approach below or a global constant.\n\n### Approach B: Nested struct\n\n```swift\nclass SingletonB {\n    \n    class var sharedInstance: SingletonB {\n        struct Static {\n            static let instance: SingletonB = SingletonB()\n        }\n        return Static.instance\n    }\n    \n}\n```\n\nHere we are using the static constant of a nested struct as a class constant. This is a workaround for the lack of static class constants in Swift 1.1 and earlier, and still works as a workaround for the lack of static constants and variables in functions.\n\n### Approach C: dispatch_once\n\nThe traditional Objective-C approach ported to Swift.\n\n```swift\nclass SingletonC {\n    \n    class var sharedInstance: SingletonC {\n        struct Static {\n            static var onceToken: dispatch_once_t = 0\n            static var instance: SingletonC? = nil\n        }\n        dispatch_once(\u0026Static.onceToken) {\n            Static.instance = SingletonC()\n        }\n        return Static.instance!\n    }\n}\n```\n\nI'm fairly certain there's no advantage over the nested struct approach but I'm including it anyway as I find the differences in syntax interesting.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhpique%2FSwiftSingleton","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhpique%2FSwiftSingleton","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhpique%2FSwiftSingleton/lists"}