ios writeToFile更改不保存 [英] ios writeToFile changes don't save

查看:86
本文介绍了ios writeToFile更改不保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ios声称该文件已被写入,但更改从未实际保存,就好像它们保留在缓冲区中一样。我需要刷新吗?

ios claims the file has been written to, but the changes never actually save, as if they remain in buffer. Do I have to flush or something?

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *myFilePath = [[NSBundle mainBundle]
                            pathForResource:@"MyFile"
                            ofType:@"txt"];

    NSLog(@"\n\nPath = \n%@", myFilePath);

    NSString *myFileContents = [NSString stringWithContentsOfFile:myFilePath
                                    encoding:NSUTF8StringEncoding
                                    error:nil];


    NSLog(@"\n\nContents = \n%@", myFileContents);

    [@"This line will be written to file" writeToFile:myFilePath
        atomically:YES encoding:NSUTF8StringEncoding error:nil];

    NSString *changedContents = [NSString stringWithContentsOfFile:myFilePath
                                    encoding:NSUTF8StringEncoding
                                    error:nil];

    NSLog(@"\n\nChanged Contents = \n%@", changedContents);
}

输出:

Path = 
/Users/hamzeh/Library/Application Support/iPhone Simulator/5.1/Applications/2B318687-A745-4CD9-85BA-52A01AB76F6E/savepersistentdata_tutorial.app/MyFile.txt

Contents = 
This is a text file.
The file will be displayed on the iPhone.
That is all for now. 

Changed Contents = 
This line will be written to file

这是我每次运行时得到的输出,因此更改实际上不会写入文件。

This is the output I get every time I run, so the changes don't actually get written to the file.

感谢您的帮助。

推荐答案

您无法编辑主包中的文件。您必须先将文件保存到应用程序文档文件夹,然后进行任何更改。

You can't edit files in your main bundle. You have to first save the file to the applications documents folder then make any changes.

这里有一些代码可以解决您的问题:

Here's some code to maybe fix your issue:

首先将文本保存到您创建的目录:

First save the text to a directory you create:

NSString *myFilePath = [[NSBundle mainBundle]
                            pathForResource:@"MyFile"
                            ofType:@"txt"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *directory = [NSString stringWithFormat:@"%@/Some_Directory", [paths objectAtIndex:0]];

// Check if the directory already exists
if (![[NSFileManager defaultManager] fileExistsAtPath:directory]) {
    // Directory does not exist so create it
    [[NSFileManager defaultManager] createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:nil];
} 

NSData *data = [[NSData dataWithContentsOfFile:myFilePath] retain];

NSString *filePath = [[directory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", [myFilePath lastPathComponent]]] retain];



NSError *error = nil;
if (![data writeToFile:filePath options:NSDataWritingFileProtectionNone error:&error]) {
    [data writeToFile:filePath atomically:NO];
}
[data release];
[filePath release];

这篇关于ios writeToFile更改不保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆