写入文件不起作用 [英] Write to file does not work

查看:104
本文介绍了写入文件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用以下代码写入文件时:

When i try to write to file with this code:

NSString *path = [[NSBundle mainBundle] pathForResource:@"default" ofType:@"txt"];
NSError *error = nil;
NSString*s = @"some text";
BOOL successful = [s writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
NSLog(@"%@",error);
NSLog(@"%d",successful);

什么都没发生。 NSLog说:

Nothing happens. NSLog says:

(null)
1


推荐答案

这是因为您正在编写主捆绑包,请尝试以下操作:

That's because you're writing over the main bundle try this:

    NSString *applicationDocumentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *storePath = [applicationDocumentsDir stringByAppendingPathComponent:@"default.txt"];

在这里,我们试图覆盖我们的应用程序文档目录。因为你不能在主捆绑包上写东西

Here we're trying to write over our app Documents directory. Because you can't write on the main bundle

这篇关于写入文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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