开发者

Reading values from a plist file

开发者 https://www.devze.com 2023-03-29 15:00 出处:网络
I have a UITextField and when the user hits a save button, the text should be saved into a plist file with the following code:

I have a UITextField and when the user hits a save button, the text should be saved into a plist file with the following code:

-(IBAction)saveButtonPressed
{
       NSError *error;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
        NSString *documentsDirectory = [paths objectAtIndex:0]; //2
        NSString *path = [documentsDirectory stringByAppendingPat开发者_JAVA技巧hComponent:@"Data.plist"]; //3

        NSFileManager *fileManager = [NSFileManager defaultManager];

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

            [fileManager copyItemAtPath:bundle toPath: path error:&error]; //6
        }
       NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
        //here add elements to data file and write data to file
        [data setObject:[inkTextField text] forKey:@"inkText"];

        BOOL didWrite=[data writeToFile: path atomically:YES];
       if(didWrite==YES)
           NSLog(@"didWrite");
       else NSLog(@"nope");
        [data release];
    }

Then I have a UITableView and I want to load the string value of plist into the cell text field with the following code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellViewController"];
    if (cell == nil) {
        // Load the top-level objects from the custom cell XIB.
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CellViewController" owner:self options:nil];
        // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
        cell = [topLevelObjects objectAtIndex:0];
    }
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
    NSString *documentsDirectory = [paths objectAtIndex:0]; //2
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Data.plist"]; //3

    NSFileManager *fileManager = [NSFileManager defaultManager];

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

        [fileManager copyItemAtPath:bundle toPath: path error:&error]; //6
    }
    NSMutableDictionary *savedInks = [[NSMutableDictionary alloc] initWithContentsOfFile: path];

    //load from savedStock example int value
    NSString *value;
    value = [[savedInks objectForKey:@"inkText"]stringValue];
    [savedInks release];
    inkTitle.text=value;
    return cell;
}

When I save, I get a the log outputs didWrite, so I know it saved properly. However when I visit the tableView, I get a crash with the following error:

2011-08-19 13:56:45.717 MyApp[36067:b303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString stringValue]: unrecognized selector sent to instance 0x4bb5fa0'

So I thought it was something to do with this line:

value = [[savedInks objectForKey:@"inkText"]stringValue];

so I tried

value = [savedInks objectForKey:@"inkText"];

but that also causes a crash but with no message. What am I doing wrong?


You should remove the 'stringValue', as you're already returning a NSString value.

In the next line, you're releasing 'savedInks', before retaining 'value'. Swap the release with the line below it and see if that works.:

NSString *value;
value = [savedInks objectForKey:@"inkText"];
inkTitle.text=value;
[savedInks release];

When savedInks is released, all of the objects within the dictionary are released as well. Setting inkTtile.text should perform a retain on value automatically.

0

精彩评论

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