将数据从字典字典加载到 Objective C 中的 iPhone 应用程序数组中

发布于 2024-08-26 20:08:31 字数 3536 浏览 2 评论 0原文

我有一个 UINavigationController,其中包含一个我想要加载一些数据的表视图。 我有一个字典 plist,其中包含每条火车线路的字典,而该字典又包含每个车站的字典,其中包含相关信息以及一个字符串 lineName。我需要收集站名称键并将它们添加到数组中以填充我的表(这是有效的)。

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

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

我是否以错误的方式处理此问题?我应该重新组织我的 plist 吗? 下面的代码使应用程序崩溃。

- (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
)

I have a UINavigationController consisting of a tableview I want to load some data into.
I have a dictionary plist containing Dictionaries for each Train Line which in turn have dictionaries for each station with the relevant information along with one string lineName. I need to collect the station Names keys and add them to an array to populate my table (This is working).

The line names are stored as a string in my lines dictionary with the key being "lineName"

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

Am I going about this the wrong way? Should I reorganise my plist?
The code below crashes the app.

- (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];
 }

Debugger console output:

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
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夕色琉璃 2024-09-02 20:08:31
 NSMutableArray *stationsOnLine = [[NSArray alloc] init];

应该可以

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

正常编译,因为 Obj-C 编译器认为这是一个 NSMutableArray,但指针实际上指向一个 NSArray 实例,因此在运行时,这就是“变异方法发送的内容”到不可变类型”错误就是这样的。通常这会显示为“无法识别的选择器”错误,但我认为内部处理数组的方式会导致另一个更神秘的错误消息。

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

Should be

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

It compiles fine because the Obj-C compiler thinks this an NSMutableArray, but the pointer is actually to an NSArray instance, so at runtime that's what the "mutating method sent to immutable type" error is all about. Normally this would show up as an "unrecognized selector" error, but I suppose the way that arrays are handled internally causes this other, more cryptic error message.

杀手六號 2024-09-02 20:08:31

您的代码对我来说看起来都很好(在 for 语句之外声明的 key 变量有点不寻常,但我认为它不会爆炸)。你没有说你看到的是哪种崩溃,但我能想到的代码崩溃的唯一原因是,如果其中一个方法在某处返回 nil,然后 nil 作为参数传递给一种不接受它的方法。也许您没有从 NSBundle 返回有效路径,也许 NSDictionary 没有从路径正确创建,类似的事情。

Your code all looks fine to me (the key variable declared outside the for statement is a little unusual, but I don't think it would blow up). You don't say what kind of crash you're seeing, but the only reason I can think of why that code would crash is if one of the methods is returning nil somewhere along the line and then nil is getting passed as an argument to a method that won't take it. Maybe you're not getting a valid path back from NSBundle, maybe the NSDictionary isn't being properly created from the path, something like that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文