【问题标题】:Store and fetch documents in/from app documents folder在应用文档文件夹中/从应用文档文件夹中存储和获取文档
【发布时间】:2013-05-17 23:22:29
【问题描述】:

我正在编写一个文本编辑器应用程序并尝试将 NSString 作为 NSData 存储在文档文件夹中的文件中,例如,我希望能够存储一个文件“myText.java”。

我的代码在模拟器上运行。但是,在设备上似乎创建了一个文件,但是当我稍后尝试从文件中加载数据时,我什么也没得到。

我必须设置一些项目设置来启用设备上的文档目录还是我的代码错误?

这是我的商店代码:

-(void) saveFile:(NSString*)file{
    if (![file isEqual:@"Empty"]) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains
        (NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *fullFilePath = [documentsDirectory stringByAppendingPathComponent:file];

        [[NSFileManager defaultManager] removeItemAtPath: fullFilePath error: nil];
        NSLog(@"File: %@", fullFilePath); 
        //Above LOGS: /var/mobile/Applications/2310F459-282C-4488-AE24-D5795168F85A/Documents/fg

        //save content to the documents directory
        NSLog(@"Saving: %@", codeView.text);
        // Logs the data i want to store
        [[codeView.text dataUsingEncoding:NSASCIIStringEncoding] writeToFile:fullFilePath atomically:YES];
    }
}

这是我的加载文件代码:

-(void) loadFile:(NSString*)filename{
    NSArray *paths = NSSearchPathForDirectoriesInDomains
    (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    //make a file name to write the data to using the documents directory:
    NSString *file = [documentsDirectory stringByAppendingPathComponent:filename];
    if ([[NSFileManager defaultManager] fileExistsAtPath:file]) {
        NSLog(@"File found");
        //Yes the file is found

        theDelegate.fileData = [NSData dataWithContentsOfFile:file];
        NSLog(@"Data:%@",[[NSString alloc] initWithData:[NSData dataWithContentsOfFile:file] encoding:NSASCIIStringEncoding]);
        // On device and simulator data is found

        [theDelegate.codeView setText:[[NSString alloc] initWithData:theDelegate.fileData encoding:NSASCIIStringEncoding]];
        //codeView does not get updated with the data.
        //NSLog([[NSString alloc] initWithData:theDelegate.fileData encoding:NSASCIIStringEncoding]);
        [theDelegate setTitle:filename];
        [theDelegate setHasOpenFile:YES];
        [theDelegate.codeView setEditable:theDelegate.hasOpenFile];
        [theDelegate.codeView setNeedsDisplay];
        [self setLanguage:filename];
    }else{

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"File error!" message:@"An error occured when trying to load the selected file." delegate:self cancelButtonTitle:@"OK!" otherButtonTitles:nil, nil];
       [alert show];
    }
}

【问题讨论】:

  • 查看writeToFile:atomically:的返回值。另外,您说您希望将文件保存为myText.java,但日志显示file 只是fg
  • 我正试图找出你哪里出错了,但顺便说一句,这段代码有点可怕。 saveFile 使用 file 作为文件名,loadFile 使用 filename 作为名称,file 作为整个路径。这是要求错误。
  • 谢谢你的意见,我会处理的

标签: ios nsfilemanager nsdocumentdirectory


【解决方案1】:

不,您不必启用设置即可使用文档目录。你的代码可能有也可能没有错误,无论如何它很难阅读而且不是很干净(很抱歉这么说。)

尝试一次做所有事情。我冒昧地重写了您的代码,以清理它。然后我用手机上的示例应用程序对其进行了测试,它运行良好。

代码重写:

-(void) saveFile:(NSString*)filename{
    if (![filename isEqual:@"Empty"]) {

        NSString *fullFilePath = [self getFullFilePath:filename];

        [[NSFileManager defaultManager] removeItemAtPath: fullFilePath error: nil];
        NSLog(@"File: %@", fullFilePath);

        //save content to the documents directory
        NSLog(@"Saving: %@", self.codeView.text);
        // Logs the data i want to store
        [[self.codeView.text dataUsingEncoding:NSASCIIStringEncoding] writeToFile:fullFilePath atomically:YES];
    }
}

上面的代码很好,添加了辅助函数之后。

-(void) loadFile:(NSString*)filename{

    NSString *fullFilePath = [self getFullFilePath:filename];

    if ([[NSFileManager defaultManager] fileExistsAtPath:fullFilePath]) {
        NSLog(@"File found");
        //Yes the file is found

        NSLog(@"Data:%@",[[NSString alloc] initWithData:[NSData dataWithContentsOfFile:fullFilePath] encoding:NSASCIIStringEncoding]);
        // On device and simulator data is found

        [theDelegate.codeView setText:[[NSString alloc] initWithData:[NSData dataWithContentsOfFile:fullFilePath] encoding:NSASCIIStringEncoding]];

        [theDelegate setTitle:filename];
        [theDelegate setHasOpenFile:YES];
        [theDelegate.codeView setEditable:theDelegate.hasOpenFile];
        [theDelegate.codeView setNeedsDisplay];
        //[self setLanguage:filename];
    }else{

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"File error!" message:@"An error occured when trying to load the selected file." delegate:self cancelButtonTitle:@"OK!" otherButtonTitles:nil, nil];
        [alert show];
    }
}

在您的旧代码中,如果引用较弱,theDelegate.fileData = [NSData dataWithContentsOfFile:file]; 可能存在问题。由于我认为您将始终在 codeView 中拥有代码,因此似乎没有必要先将其存储在成员变量中。此外,这可能会导致更多错误。

以下是一个辅助函数,因此您不要在两个函数中执行完全相同的操作,因为这可能会导致错误。

-(NSString*) getFullFilePath:(NSString*)filename {
    NSArray *paths = NSSearchPathForDirectoriesInDomains
    (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullFilePath = [documentsDirectory stringByAppendingPathComponent:filename];

    return fullFilePath;

}

【讨论】:

  • 您可能是对的,跳过 NSData 步骤使我的代码按预期运行。 BR
【解决方案2】:

您是否注意传递给loadFile:saveFile: 的文件名完全相同相同,包括大小写? Mac OS X(以及扩展的 iOS 模拟器)使用不区分大小写的文件名,而 iOS 区分大小写并将 SomeFile.txtsomefile.txt 视为两个不同的东西。

【讨论】:

  • 这是评论/问题,不是答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-17
  • 2011-04-13
  • 1970-01-01
  • 2012-12-01
  • 1970-01-01
  • 2015-02-27
相关资源
最近更新 更多