How Objective-C singleton should implement init method?

前端 未结 4 1205
暗喜
暗喜 2020-12-05 11:12

I read a couple of amazing resources on singletons in Obj-C:

  1. SO question: What does your Objective-C singleton look like?
  2. Friday Q&A: Care and Fee
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 12:07

    To make the init/new methods unavailable for callers of your singleton class you could use the NS_UNAVAILABLE macro in your header file:

    - (id)init NS_UNAVAILABLE; 
    + (id)new NS_UNAVAILABLE; 
    
    + (instancetype)sharedInstance;
    

提交回复
热议问题