本文介绍了静态NSDictionary * const letterValues = @ {.....}在一个方法中无法编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在:

   - (void)awakeFromNib 
{
[ super awakeFromNib];

static NSDictionary * const letterValues = @ {
@A:@ 1,
@B:@ 4,
@C:@ 4,
// ...
@X:@ 8,
@Y:@ 3,
@Z:@ 10,
};

NSString * randomLetter = [kLetters substringWithRange:[kLetters rangeOfComposedCharacterSequenceAtIndex:arc4random_uniform(kLetters.length)]];
int letterValue = [letterValues [randomLetter] integerValue];

_smallLetter.text = _bigLetter.text = randomLetter;
_smallValue.text = _bigValue.text = [NSString stringWithFormat:@%d,letterValue];
}

不幸的是,这给了我编译错误初始化元素不是编译 - 时间常数,我必须删除 static 关键字才能在Xcode中编译我的应用程序(此处语法。



但为什么我不能在这里使用 static



我认为这样做是合适的,以确保我的 letterValues 常量只设置一次?

解决方案

您只能在初始化期间使用常量设置静态变量。 @ {}创建一个对象,因此不是常量。



请改为:

   - (void)awakeFromNib 
{
[super awakeFromNib];

static NSDictionary * letterValues = nil;

static dispatch_once_t onceToken;
dispatch_once(& onceToken,^ {
letterValues = @ {
@A:@ 1,
@B:@ 4,
@ C:@ 4,
// ...
@X:@ 8,
@Y:@ 3,
@Z:@ 10 ,
};
});


...
}

一些这里的其他答案建议检查nil而不是一次调度,但这可能会导致同时创建多个tile(通过线程)时出现问题。 dispatch_once实现了所需的锁定。


In a word game for iPhone:

I'm trying to use the following code in my custom view Tile.m:

- (void)awakeFromNib
{
    [super awakeFromNib];

    static NSDictionary* const letterValues = @{
                                         @"A": @1,
                                         @"B": @4,
                                         @"C": @4,
                                         // ...
                                         @"X": @8,
                                         @"Y": @3,
                                         @"Z": @10,
                                         };

    NSString* randomLetter = [kLetters substringWithRange:[kLetters rangeOfComposedCharacterSequenceAtIndex:arc4random_uniform(kLetters.length)]];
    int letterValue = [letterValues[randomLetter] integerValue];

    _smallLetter.text = _bigLetter.text = randomLetter;
    _smallValue.text = _bigValue.text = [NSString stringWithFormat:@"%d", letterValue];
}

Unfortunately this gives me compile error Initializer element is not a compile-time constant and I have to remove the static keyword to get my app compile in Xcode (here fullscreen):

I think I initialize the NSDictionary correctly - by using the new Objective-C Literals syntax.

But why can't I use static here?

I thought it would be appropriate here to make sure that my letterValues constant is set only once?

解决方案

You can only set a static variable during initialization with a constant. @{} creates an object, thus not a constant.

Do this instead:

- (void)awakeFromNib
{
    [super awakeFromNib];

    static NSDictionary* letterValues = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        letterValues = @{
          @"A": @1,
          @"B": @4,
          @"C": @4,
          // ...
          @"X": @8,
          @"Y": @3,
          @"Z": @10,
          };
    });


    ...
}

Some other answers here suggest a check for nil instead of dispatch once, but that can cause issues when creating multiple tiles at the same time (via threads). dispatch_once implements the required locking.

这篇关于静态NSDictionary * const letterValues = @ {.....}在一个方法中无法编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 07:38
查看更多