读入加载:

 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文件:



错误:


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


有任何想法吗?干杯。

最佳答案

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

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


等等。

(为什么您认为必须进行此方法调用?)

关于objective-c - 读取plist文件崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12267861/

10-10 20:28