static NSDictionary* const letterValues = @{ … } in a method does not compile

后端 未结 4 1520
南方客
南方客 2021-02-05 13:20

In a word game for iPhone:

\"app

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

4条回答
  •  忘掉有多难
    2021-02-05 13:53

    NSDictionary objects can't be created at compile time. However, if you need a static object, you can create one. You can, for example, use the initialize method, like this:

    static NSDictionary* letterValues;
    
    + (void)initialize
    {
        if (self == [MyClass class]) {
            letterValues = @{
                             @"A": @1,
                             @"B": @4,
                             @"C": @4,
                             @"X": @8,
                             @"Y": @3,
                             @"Z": @10,
                             };
        }
    }
    

    The if statement is there to prevent multiple calls of your code in MyClass subclasses.

提交回复
热议问题