【发布时间】:2012-10-19 11:09:16
【问题描述】:
您好,我正在尝试从文件中加载数据
此功能将数据保存到文档文件夹中的文件
- (IBAction)saveUser:(id)sender{
NSString *name = [nameField stringValue];
NSString *weight = [weightField stringValue];
NSDate *date = [datePick dateValue];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:[nameField stringValue] forKey:@"name"];
[dict setValue:[weightField stringValue] forKey:@"weight"];
[dict setValue:date forKey:@"date"];
[dict writeToFile:name atomically:YES];
}
但是,当我尝试加载文件并从中获取数据时,我无法做到,谁能告诉我这是如何完成的。
编辑以显示 loadFunction
-(IBAction)loadUser:(id)sender{
NSString *weight = [weightField stringValue];
NSDate *date = [datePick dateValue];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *loadPath = [documentsDirectory stringByAppendingPathComponent:name];
NSMutableDictionary *savedData = [[NSMutableDictionary alloc] initWithContentsOfFile: name];
NSLog(@"%@", weight);
}
【问题讨论】:
-
文件确实被创建了,但我不知道如何在目标 c 中找到文件并从中读取数据
-
不过,您只向我们展示了一半的代码。你能告诉我们你用来从文件中读取数据的代码吗?
-
在
loadUser中name是什么?您在saveUser的本地上下文中定义了name,但我没有看到它在loadUser中定义...
标签: objective-c macos cocoa load save