Singleton in iOS Objective C doesn't prevent more than one instance

后端 未结 9 1555
长情又很酷
长情又很酷 2020-11-29 08:53

I know there are several threads on this, but none answer my questions.

I\'ve implemented my singleton class like this (being aware of the controversy about singleto

9条回答
  •  粉色の甜心
    2020-11-29 09:19

    @VeryPoliteNerd just mark the init and new methods as unavailable on the .h:

    - (instancetype)init __attribute__((unavailable("Use +[MyClass sharedInstance] instead")));
    
    + (instancetype)new __attribute__((unavailable("Use +[MyClass sharedInstance] instead")));
    

    This will cause the compiler to complain if a caller tries to manually instantiate this objects

提交回复
热议问题