gpt4 book ai didi

objective-c - 读取 plist 文件崩溃

转载 作者:行者123 更新时间:2023-11-28 19:15:24 31 4
gpt4 key购买 nike

读入加载:

 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]: unrecognized selector sent to instance 0x6aa38f0 2012-09-04 17:03:40.362 app[4849:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString stringValue]: unrecognized selector sent to instance 0x6aa38f0'

有什么想法吗?干杯。

最佳答案

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

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

等等。

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

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

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com