LoginSignup
0
1

Swift での Singleton Pattern とその注意点

Posted at

最小限の実装

class SomeManager {
    static let shared = SomeManager()
}

init メソッドを制限

設計によって init をさせたくないばいは -

class SomeManager {
    static let shared = SomeManager()
    private init() {}
}

必ず class にすること

struct にすると、アサインするたびにコピーされ、新たなオブジェクトが作られるて、 singleton ではなくなりますので要注意です

struct SomeManager {
    static let shared = SomeManager()
    private init() {}
}
let someManager = SomeManager.shared
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1