我有一個包含tableview的UINavigationController我想要加載一些數據。 我有一個詞典plist包含每個火車線的字典,每個火車線又有字典爲每個站與相關信息以及一個字符串lineName。我需要收集站名稱鍵並將它們添加到數組來填充我的表(這是工作)。將字典詞典中的數據加載到目標C中的數組中,用於iphone應用程序
線路名稱都存儲在我行一個字符串鍵值爲「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
)
我想我把所有的實驗弄亂了代碼,明天早上我會仔細看看並修改它。 不確定合適的做法是將問題保留並明天進行編輯,或者如果明天需要刪除並開始新建問題? 謝謝。 – Khat 2010-03-31 04:49:36