# Swift 3.0 Design Patterns: Singleton

Language: en
Canonical: [Swift 3.0 Design Patterns: Singleton](<https://allluckly.cn/en/journal/2016-10-13-swift-3-0-design-patterns-singleton/>)
中文：[中文 HTML](<https://allluckly.cn/%E6%8A%95%E7%A8%BF/tougao67>)
English: [English HTML](<https://allluckly.cn/en/journal/2016-10-13-swift-3-0-design-patterns-singleton/>)
Published: 2016-10-13T00:00:00+08:00
Modified: 2026-10-23T10:34:15+08:00
Author: Sindri

Introduces the uses and trade-offs of Swift singletons, creating shared instances with a static class constant or a global constant. It covers private initializers, access across modules, and the need to consider thread safety for shared mutable state.

Originally credited to Sindri; edited and archived by Bison.

[Source listed in the original](<http://www.jianshu.com/p/6c9423672d54>)

English translation of the [Chinese original](<https://allluckly.cn/%E6%8A%95%E7%A8%BF/tougao67>), translated on 2026-10-23. The article body uses automated translation; app descriptions, historical reading notes and video subtitles receive separate editorial checks. Original publication dates, attribution, code and version notes are retained.

**Before you read** This technical note dates from 2015–2017. The examples have not been rerun as part of this archive update. The article explicitly discusses singleton syntax after Swift 3.0 and changes to the dispatch\_once API.

![1](<https://allluckly.cn/images/blog/tuogao/tougao66/1.png>)

Editor: [Bison](<https://allluckly.cn/en/>)  
 Source: [Sindri’s Notes](<http://www.jianshu.com/p/6c9423672d54>)

Design patterns are classified, widely known, repeatedly used solutions distilled from code-design experience. The GoF proposed 23 patterns; this series implements them in Swift.

## Overview

A class with exactly one instance throughout an app’s lifetime is a singleton. Sharing that object across modules is the Singleton pattern.

The Singleton pattern has these advantages:

Multiple modules share one object, keeping data unique. Centralised logic offers flexibility. iOS itself provides singletons such as NSNotificaitonCenter and UIApplication. Swift mainly uses two singleton-creation approaches. I usually store user data in a singleton for access across modules:

- Approach 1: declare a static constant inside the class and make its initialiser private.

```

    class UserInfo {

        static let sharedInfo = UserInfo()

        private init() {}
    }

```

- Approach 2: use a global constant object.

```

    let singleton = UserInfo()

        class UserInfo {
            class var sharedInfo : UserInfo {
                return singleton
            }
    }

```

Developers coming from Objective-C may prefer dispatch\_once, but it is no longer available in Swift3.0. Apple already uses dispatch\_once in static let implementation to ensure only one instance exists.

## Summary

A singleton keeps data unique during the app’s lifetime and avoids duplicate memory usage, but a large singleton itself becomes a burden. Access also raises thread-safety concerns, requiring appropriate locking for stability.

---

Recommended reading: [Implement an iOS launch-screen advertisement in minutes](<https://allluckly.cn/en/journal/2016-06-12-adding-a-launch-advertisement-to-an-ios-app/>)

