【问题标题】:Objective-C save and load filesObjective-C 保存和加载文件
【发布时间】: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 中定义...

标签: objective-c macos cocoa load save


【解决方案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中的文件路径...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-22
      • 1970-01-01
      • 1970-01-01
      • 2015-03-18
      • 1970-01-01
      • 1970-01-01
      • 2021-03-28
      • 2012-09-04
      相关资源
      最近更新 更多