代码之家  ›  专栏  ›  技术社区  ›  Khat

在iphone应用程序的objective c中将字典中的数据加载到数组中

  •  1
  • Khat  · 技术社区  · 14 年前

    我有一个uinavigationcontroller,它包含一个要加载一些数据的tableview。 我有一个字典plist,它包含每一条列车线的字典,而每一个车站的字典又包含相关信息和一个字符串行名。我需要收集station names键并将它们添加到数组中以填充我的表(这正在工作)。

    行名作为字符串存储在“我的行”字典中,键为“行名”

    Root->|
          |
          |->TrainLine1(Dictionary)->|
          |                          |-> lineName (String)
          |                          |-> Station1 (Dictionary)
          |                          |-> Station2 (Dictionary)
          |
          |
          |->TrainLine2(Dictionary)->|
          |                          |-> lineName (String)
          |                          |-> Station1 (Dictionary)
          |                          |-> Station2 (Dictionary)
    

    我是不是走错了路?我应该重组我的公司吗? 下面的代码使应用程序崩溃。

    - (void)viewDidLoad {
         NSString *path = [[NSBundle mainBundle] pathForResource:@"lineDetails" ofType:@"plist"];
         NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:path];
         NSDictionary *lineDictionary = [[NSDictionary alloc] initWithDictionary:[dictionary objectForKey:stationNameKey]];
         NSMutableArray *stationsOnLine = [[NSArray alloc] init];
    
         NSString *key;
    
         for (key in lineDictionary) {
    
             NSLog(@"Adding this in array:%@", key);
             //NSString *key2;
             NSString *nameToTry = [NSString stringWithString:key];
             NSLog(@"nameToTry : %@", nameToTry);    
             //NSMutableDictionary *stationDictionary = [[NSDictionary alloc] init];
             if (![key isEqualToString: @"lineName"]) 
             {
                 //NSMutableDictionary *stationDictionary = [[NSDictionary alloc] init];
                // NSLog(@"Yes");
                 //NSMutableDictionary *tempDict = [[NSDictionary alloc] initWithDictionary:[lineDictionary objectForKey:key]];
                 NSMutableDictionary *stationDictionary = [[NSDictionary alloc] initWithDictionary:[lineDictionary objectForKey:key]];
                 //stationDictionary = tempDict;
                 NSLog(@"Object for key--  %@",[stationDictionary objectForKey:kStationName]);
                 [stationsOnLine addObject:[stationDictionary objectForKey:kStationName]];
                 [stationDictionary release];
                 //[tempDict release];
             }
             /*
             for (key2 in stationDictionary)
             {
                 NSLog(@"Adding this in station array:%@", key);
             }
              */
    
         }
         stationNames = stationsOnLine;
         //[stationDictionary release];
    
         [stationsOnLine release];
         [lineDictionary release];
         [dictionary release];
     }
    

    调试器控制台输出:

    2010-03-31 00:42:39.842 AMT_Schedule[8395:207] did SelectRow Array contents:deux-montagnes
    2010-03-31 00:42:39.844 AMT_Schedule[8395:207] Adding this in array:sunnybrooke
    2010-03-31 00:42:39.844 AMT_Schedule[8395:207] nameToTry : sunnybrooke
    2010-03-31 00:42:39.845 AMT_Schedule[8395:207] Object for key--  Sunnybrooke
    2010-03-31 00:42:39.846 AMT_Schedule[8395:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'
    2010-03-31 00:42:39.847 AMT_Schedule[8395:207] Stack: (
        29320283,
        2521638153,
        29404219,
        29404058,
        107345,
        107124,
        17393,
        3270466,
        3263806,
        3306080,
        3302106,
        3308563,
        3289798,
        3310951,
        3289447,
        15819,
        3066438,
        3049604,
        303530,
        29104832,
        29101128,
        37410325,
        37410522,
        2793391,
        8628,
        8482
    )
    
    2 回复  |  直到 14 年前
        1
  •  4
  •   Ian Henry    14 年前
     NSMutableArray *stationsOnLine = [[NSArray alloc] init];
    

    应该是

     NSMutableArray *stationsOnLine = [[NSMutableArray alloc] init];
    

    它编译得很好,因为obj-c编译器认为 NSMutableArray ,但指针实际上指向 NSArray 实例,所以在运行时这就是“mutating method sent to immutable type”错误的意义所在。通常这会显示为一个“无法识别的选择器”错误,但我认为数组的内部处理方式会导致另一个更为神秘的错误消息。

        2
  •  0
  •   Chuck    14 年前

    你的代码在我看来都很好 key for 声明有点不寻常,但我不认为会爆炸)。你不会说你看到了什么样的崩溃,但我能想到的唯一原因是,如果其中一个方法在某一行返回nil,然后nil作为参数传递给一个不接受它的方法。可能您没有从nsbundle获取有效的路径,或者nsdictionary没有从该路径正确创建,诸如此类。