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

前端 未结 9 1883
甜味超标
甜味超标 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:37

    Why don't use :

    +(NSString*)titleForSection:(NSInteger)section {
        return (@[ @"title1", @"title2", @"title3" ])[section];
    }
    
    0 讨论(0)
  • 2020-12-12 21:40

    You can also do something like this:

    static NSString * const strings[] = {
            [0] = @"string_1",
            [1] = @"string_2",
            [2] = @"string_3",
            [3] = @"string_4",
            // ...
        };
    

    It's not an NSArray but you can access NSStrings like this strings[n]

    0 讨论(0)
  • 2020-12-12 21:44

    I wonder, if the following would be a good way (answering my own question):

    #import "MasterViewController.h"
    #import "DetailViewController.h"
    
    static const NSArray *_titles;
    
    @interface MasterViewController () {
        NSMutableArray *_objects;
        NSMutableArray *_yourMove;
        NSMutableArray *_theirMove;
        NSMutableArray *_wonGames;
        NSMutableArray *_lostGames;
        NSMutableArray *_options;
    }
    @end
    
    @implementation MasterViewController
    
    + (void)initialize
    {
        // do not run for derived classes
        if (self != [MasterViewController class])
            return;
    
        _titles = @[
            @"Your Move",
            @"Their Move",
            @"Won Games",
            @"Lost Games",
            @"Options"
        ];
    }
    

    This way the const NSArray is initalized once and right before I need it (in the MasterViewController class). And the self check prevents this method from running again - when some inheriting class does not implement its own +initialize method.

    0 讨论(0)
  • 2020-12-12 21:48

    You can init it in class method +initialize

    static NSArray *_titles_1;
    
    @implementation MasterViewController
    + (void)initialize {
        _titles_1 = @[
            @"Your Move",
            @"Their Move",
            @"Won Games",
            @"Lost Games",
            @"Options"
        ];
    }
    @end
    
    0 讨论(0)
  • 2020-12-12 21:50

    You should declare your static array above @implementation.

    static NSArray *titles_1 = nil;
    
    @implementation ...
    

    And define it in init or awakeFromNib or any other method like viewDidLoad, applicationDidFinishLaunching wherever you want.

    - (void)initMethod{ //change the method name accordingly
    
        if (titles_1 == nil){
            [self setTitles_1:@[ @"Your Move", @"Their Move", @"Won Games", @"Lost Games", @"Options" ]];
        }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题