我正确使用NSDictionary吗? (取自plist)

时间:2016-05-05 08:18:28

标签: ios objective-c xcode nsdictionary

    -(void)setUpHorizontal {
        int i = 1;
        while (_data[[NSString stringWithFormat:@"Horizontal%i", i]] != nil) {
            NSDictionary* horizontal = _data[[NSString stringWithFormat:@"Horizontal%i", i]];
            _column = horizontal[@"Column"];
            _row = horizontal[@"Row"];
            NSInteger column = [_column intValue];
            NSInteger row = [_row intValue];
            [super setHBarCellState:HorizontalState forHBarCol:column andHBarRow:row];
            i++;
        }
    }

     -(instancetype)initWithDestinationIndex:(NSUInteger)levelsIndex {
         self = [super init];
         if (self) {
            NSString* filepath = [[NSBundle mainBundle]
                          pathForResource:@"Levels" ofType:@"plist"];
            NSDictionary *levels = [NSDictionary dictionaryWithContentsOfFile:filepath];
            NSArray *levelsArray = levels[@"LevelsData"];
            _data = levelsArray[levelsIndex];
         }
     return self;
     }

首先,stringWithFormat:方法正在查找名为&strong;水平%i" 的 NSDictionary *的字符串。在while循环结束之前,我需要再经历2个(Horizo​​ntal2,Horizo​​ntal3)。 _column和_row是NSNumber实例变量(其值取自具有关键标题的#14;列"以及"行")。

每次这样的循环运行时,这两个键都是各自字典的子子句(在本例中为Horizo​​ntal%i)。

_data是主要的NSDictionary * 。我使用它作为一个实例变量,虽然我甚至不知道它是否可能(或者说有效)。我这样使用它是因为那些' setUpHorizo​​ntal'以及我需要访问_data的所有其他方法。

想象一下'水平'代表一个国家。那么我还有其他14个州执行相同的任务。最终结果是将特定状态的特定列和行值输入到“集合”中.CellState:for ... Col:and ... Row:'方法。这样,通过我构建的其他方法,它们各自的图像可以显示在屏幕上。

如果有任何疑问,请与我们联系。我只是想知道你看到的代码是否真的在做任何有价值的事情。

以下是plist:                                         LevelsData                           StoneOne                              柱                 2                 行                 2                          StoneTwo                              柱                 3                 行                 4                          目标                              柱                 0                 行                 1                          Horizo​​ntal1                              柱                 2                 行                 1                          Horizo​​ntal2                              柱                 0                 行                 0                          Horizo​​ntal3                              柱                 0                 行                 3                          GhostHorizo​​ntal1                              柱                 1                 行                 3                          GhostHorizo​​ntal2                              柱                 4                 行                 0                          Vertical1                              柱                 3                 行                 0                          Vertical2                              柱                 1                 行                 2                          Vertical3                              柱                 3                 行                 4                          Intersector1                              柱                 0                 行                 3                          UP1                              柱                 1                 行                 2                          DOWN1                              柱                 3                 行                 3                          LEFT1                              柱                 0                 行                 0                          LEFT2                              柱                 2                 行                 1                          Left3                              柱                 1                 行                 3                          TopR1                              柱                 3                 行                 0                           

0 个答案:

没有答案