静态NSArray字符串

静态NSArray字符串

本文介绍了静态NSArray字符串 - 在View Controller中初始化的方式/位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Master-Detail应用程序中,我想显示一个TableView,其中有5个部分标题为:

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


  1. 你的移动

  2. 他们的行动

  3. 赢得游戏

  4. 失去的游戏

  5. 选项

  1. Your Move
  2. Their Move
  3. Won Games
  4. Lost Games
  5. Options

所以我在Xcode 5.0.2中创建了一个空白的Master-Detail应用程序,然后在它的MasterViewController.m(这是一个UITableViewController)中我创建了试图实现该方法:

So I create a blank Master-Detail app in Xcode 5.0.2 and then in its MasterViewController.m (which is a UITableViewController) I'm trying to implement the method:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return _titles[section];
}

我的问题是如何初始化NSArray _titles?

My question is however how to init the NSArray _titles?

我正在尝试使用MasterViewController.m:

I'm trying in the MasterViewController.m:

#import "MasterViewController.h"
#import "DetailViewController.h"

static NSArray *_titles_1 = @[
    @"Your Move",
    @"Their Move",
    @"Won Games",
    @"Lost Games",
    @"Options"
];

@interface MasterViewController () {
    NSMutableArray *_games;

    NSArray *_titles_2 = @[
                         @"Your Move",
                         @"Their Move",
                         @"Won Games",
                         @"Lost Games",
                         @"Options"
    ];
}
@end

@implementation MasterViewController

- (void)awakeFromNib
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        self.clearsSelectionOnViewWillAppear = NO;
        self.preferredContentSize = CGSizeMake(320.0, 600.0);
    }
    [super awakeFromNib];
}

- (void)viewDidLoad
{
     ....
}

但上面的两次尝试都给出了语法错误:

but both tries above give me syntax errors:

更新:

令我惊讶的是,对于这个简单的问题有很多建议,但作为iOS / Objective-C新手,我不确定,哪种解决方案最合适。

To my surprise there are many suggestions for this simple question, but as an iOS/Objective-C newbie I'm not sure, which solution is most appropriate.

dispatch_once - 在多线程应用程序中执行某些操作不是运行时操作吗?这不是太过分了吗?我期待一个用于启动const数组的编译时解决方案...

dispatch_once - isn't it a runtime operation to execute something once in a multi-threaded app? Isn't it overkill here? I was expecting a compile-time solution for initiating a const array...

viewDidLoad - 当我的应用程序更改时在背景和前景之间,是不是不必要一次又一次地启动我的const数组?

viewDidLoad - when my app changes between background and foreground, wouldn't it unnecessary initiate my const array again and again?

我不应该更好地设置 NSArray in awakeFromNib (因为我为所有ViewControllers使用stroyboard场景)?或者也许在 initSomething (是正确的方法 initWithStyle ?)

Shouldn't I better set the NSArray in awakeFromNib (since I use stroyboard scenes for all my ViewControllers)? Or maybe in initSomething (is the correct method initWithStyle?)

推荐答案

编写一个返回数组的类方法。

Write a class method that returns the array.

+ (NSArray *)titles
{
    static NSArray *_titles;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _titles = @[@"Your Move",
                    @"Their Move",
                    @"Won Games",
                    @"Lost Games",
                    @"Options"];
    });
    return _titles;
}

然后您可以在任何需要的地方访问它,如下所示:

Then you can access it wherever needed like so:

NSArray *titles = [[self class] titles];

这篇关于静态NSArray字符串 - 在View Controller中初始化的方式/位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 11:51