Objective-C 保存和加载文件

Posted

技术标签:

【中文标题】Objective-C 保存和加载文件【英文标题】:Objective-C save and load files 【发布时间】: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 中找到文件并从中读取数据 不过,您只向我们展示了一半的代码。你能告诉我们你用来从文件中读取数据的代码吗? loadUsername 是什么?您在saveUser 的本地上下文中定义了name,但我没有看到它在loadUser 中定义... 【参考方案1】:

修正你的答案。这是一个有效的代码,你几乎没有给出文件名。

NSString *filename = [documentsDirectory stringByAppendingPathComponent:@"file.txt"];

在你写文件的那一行:

[dict writeToFile:name atomically:YES];

你必须把名字改成文件名,像这样

[dict writeToFile:filename atomically:YES];

在你的加载数据方法中:

NSString *filename = [documentsDirectory stringByAppendingPathComponent:@"file.txt"];

NSMutableDictionary *savedData = [[NSMutableDictionary alloc] initWithContentsOfFile:filename]; 

NSLog(@"The weight is : %@", [savedData valueForKey:@"weight"]);

【讨论】:

【参考方案2】:

以下内容似乎符合您的预期(减去代码中的 UI 元素依赖项):

#import <Foundation/Foundation.h>

static NSString *pathToDocuments(void) 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [paths objectAtIndex:0];


int main(int argc, char *argv[]) 
    NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];

    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setValue:@"SomeName" forKey:@"name"];
    [dict setValue:@"SomeWeight" forKey:@"weight"];
    [dict setValue:[NSDate date] forKey:@"date"];

    NSString *filePath = [pathToDocuments() stringByAppendingPathComponent:[dict objectForKey:@"name"]];
    [dict writeToFile:filePath atomically:YES];

    [dict release];  dict = nil;

    NSLog(@"%s - Confirming dict is nil: %@",__FUNCTION__,dict);

    dict = [[NSDictionary dictionaryWithContentsOfFile:filePath] mutableCopy];
    NSLog(@"%s - weight = %@",__FUNCTION__,[dict objectForKey:@"weight"] );

    [p release];

这会将以下内容打印到控制台:

2012-10-19 06:40:38.691 Untitled[10633:707] main - Confirming dict is nil: (null)
2012-10-19 06:40:38.693 Untitled[10633:707] main - weight = SomeWeight

编辑:

也就是说,我认为问题可能是loadUser中的文件路径...

【讨论】:

以上是关于Objective-C 保存和加载文件的主要内容,如果未能解决你的问题,请参考以下文章

Objective-c 保存原始文本

保存一个 Object[][] 并加载它

从保存的文件初始化类

如何保存和加载 HashMap<String,Object>?

如何在 Objective-C 的 NSDocument 目录中保存和获取多个 pdf 文件?

在 Android APK 2.2 及更高版本中保存和加载 Object[] 信息