从plist文件中读取值

时间:2011-08-19 19:03:56

标签: objective-c cocoa-touch

我有一个UITextField,当用户点击保存按钮时,应该使用以下代码将文本保存到plist文件中:

-(IBAction)saveButtonPressed
{
       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 *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];
    }

然后我有一个UITableView,我想用以下代码将plist的字符串值加载到单元格文本字段中:

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

当我保存时,我得到一个日志输出didWrite,所以我知道它保存得当。但是,当我访问tableView时,出现以下错误导致崩溃:

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'

所以我认为这与这一行有关:

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

所以我试过

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

但这也会导致崩溃但没有消息。我究竟做错了什么?

1 个答案:

答案 0 :(得分:2)

您应该删除'stringValue',因为您已经返回了NSString值。

在下一行中,您在保留'value'之前释放'savedInks'。将释放与其下方的线交换,看看是否有效。:

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

发布savedInks后,字典中的所有对象也会被释放。设置inkTtile.text应该自动执行值保留。

相关问题