阅读plist文件崩溃

时间:2012-09-04 16:39:52

标签: objective-c ios plist read-write

读入加载:

 NSFileManager *fileManager = [NSFileManager defaultManager];

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

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

//load in text fields.
NSMutableDictionary *savedData = [[NSMutableDictionary alloc] initWithContentsOfFile: path];

nameField.text = [[savedData objectForKey:@"name"] stringValue];
locationTextField.text = [[savedData objectForKey:@"location"] stringValue];
sectorTextField.text = [[savedData objectForKey:@"sector"] stringValue];

点击按钮点击:

    - (IBAction)writingButton:(id)sender
{
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];

    //[data setObject:[NSNumber numberWithInt:value] forKey:@"value"];
    [data setObject:[NSString stringWithString:nameField.text] forKey:@"name"];
    [data setObject:[NSString stringWithString:locationTextField.text] forKey:@"location"];
    [data setObject:[NSString stringWithString:sectorTextField.text] forKey:@"sector"];
}

plist文件:

enter image description here

错误:

  

2012-09-04 17:03:40.360 app [4849:c07] - [__ NSCFString stringValue]:   无法识别的选择器发送到实例0x6aa38f0 2012-09-04   17:03:40.362应用程序[4849:c07] ***由于未被捕获而终止应用程序   异常'NSInvalidArgumentException',原因:' - [__ NSCFString   stringValue]:无法识别的选择器发送到实例0x6aa38f0'

有什么想法吗?欢呼声。

1 个答案:

答案 0 :(得分:4)

字典直接存储NSString对象 - 不需要调用名为- stringValue的(不存在)方法。只需写下

nameField.text = [savedData objectForKey:@"name"];

等等。

(为什么你觉得这个方法调用是必要的?)