Static NSArray of strings - how/where to initialize in a View Controller

前端 未结 9 1915
甜味超标
甜味超标 2020-12-12 21:16

In a Master-Detail app I\'d like to display a TableView with 5 sections titled:

  1. Your Move
  2. Their Move
  3. Won Games
  4. Lost Games
  5. O
9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-12 21:51

    dispatch_once works. It works in complicated cases, and it works in simple cases. So to be consistent, the best thing is to use it in all cases. That makes it for example a lot easier when you replace all your constant strings with calls to NSLocalizedString (). No code change needed.

    Doing things in + (void)initialize isn't really wrong, but I have had situations where I first wanted to configure a class before really using it, and initialize is of course called just before any possible configuration method would start executing. And there are situations where you don't really have a class context. dispatch_once will always work.

提交回复
热议问题