2011-08-24 56 views
0

這是我第一次使用plist作爲存儲我的應用程序的一些數據的方式。我現在的問題是我的應用程序正在給出EXC_BAD_ACCESS錯誤。在我閱讀plist的方法中,它給了我summary unavailable。但是NSMutableDictionary給了我兩個關鍵/配對,我的plist只有兩個值來學習如何使用plist(現在)。我有一種感覺,由於此摘要不可用, I got the EXC_BAD_ACCESS`錯誤?plist輸出值的問題

-(void)readFile{ 
    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1 
    NSString *documentsDirectory = [paths objectAtIndex:0]; //2 
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"EventAddress.plist"]; //3 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    if (![fileManager fileExistsAtPath: path]) //4 
    { 
     NSString *bundle = [[NSBundle mainBundle] pathForResource:@"EventAddress" ofType:@"plist"]; // 5 

     [fileManager copyItemAtPath:bundle toPath: path error:&error]; //6 
    } 

    NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path]; 

    //load from savedStock both addr and event are NSString 
    addr = [savedStock objectForKey:@"Address"]; 
    event = [savedStock objectForKey:@"Event"]; 

    [savedStock release]; 
} 

任何幫助將不勝感激。

更新1

Where the error occurred was at the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    ... 
    ... 
    // EXC_BAD_ACCESS happened at this line 
    [[cell textLabel] setText:addr]; 
} 

但當@Jeff告訴我保留地址和事件有人居住。現在我的新EXC_BAD_ACCESS來自main.m文件: int retVal = UIApplicationMain(argc, argv, nil, nil);

現在可能出了什麼問題?

UPDATE 2 這是我學習如何使用plist的網站。 plist tutorial

+0

哪一行獲得EXC_BAD_ACCESS? – progrmr

+0

對不起,沒有正確寫出問題。 addr和event都假設顯示在'UITableViewCell'' [[cell textLabel] setText:addr];'我會嘗試@Jeff的回覆,然後更新這個問題。感謝您的快速回復! –

回答

1

您是否在此之後使用addrevent的值?他們沒有被保留,所以當savedStock被分配時,他們也會被取消。如果這些是實例變量,則在將它們從字典中拉出時保留它們。

+0

好的,我現在就做,並測試一下。 –