Create singleton using GCD's dispatch_once in Objective-C

后端 未结 10 2460
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 07:26

If you can target iOS 4.0 or above

Using GCD, is it the best way to create singleton in Objective-C (thread safe)?

+ (instancetype)sharedInstance
{
          


        
10条回答
  •  孤城傲影
    2020-11-22 07:47

    Dave is correct, that is perfectly fine. You may want to check out Apple's docs on creating a singleton for tips on implementing some of the other methods to ensure that only one can ever be created if classes choose NOT to use the sharedFoo method.

提交回复
热议问题