开发者

problems with the plist output values

开发者 https://www.devze.com 2023-03-30 01:22 出处:网络
It is my first time using a plist as a way to store a litt开发者_运维技巧le data for my application. My problem now is my application is giving a EXC_BAD_ACCESS error. In my method to read the plist,

It is my first time using a plist as a way to store a litt开发者_运维技巧le data for my application. My problem now is my application is giving a EXC_BAD_ACCESS error. In my method to read the plist, it gave me summary unavailable. But the NSMutableDictionary gave me 2 key/paired, which my plist only had 2 values just to learn how to use plist (for now). I have a feeling, due to this Summary Unavailable, I got theEXC_BAD_ACCESS` error?

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

Any help would be greatly appreciated.

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

But that was settled when @Jeff told me to retain the addr and event. Now my new EXC_BAD_ACCESS is coming from the main.m file: int retVal = UIApplicationMain(argc, argv, nil, nil);

What could have gone wrong now?

UPDATE 2 This is the site I learnt how to use plist. plist tutorial


Are you using the values of addr and event after this? They aren’t being retained, so when savedStock is dellocated, they will be too. If those are instance variables, retain them when you pull them out of the dictionary.

0

精彩评论

暂无评论...
验证码 换一张
取 消