Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 写入文件不起作用_Ios_Objective C_Cocoa_Nsstring_Writetofile - Fatal编程技术网

Ios 写入文件不起作用

Ios 写入文件不起作用,ios,objective-c,cocoa,nsstring,writetofile,Ios,Objective C,Cocoa,Nsstring,Writetofile,当我尝试使用以下代码写入文件时: 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];

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

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说:

(null)
1

这是因为您要在主捆绑包上进行写入,请尝试以下操作:

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

在这里,我们试图写在我们的应用程序文档目录。因为您无法在主捆绑包上写入

,所以听起来好像没有错误,而且成功了。“什么都没发生”是什么意思?@iOSGuru248我不能确定,但听起来很像他希望“某个文本”字符串被写入他指定的路径,或者如果不成功,则是一个错误。我认为这应该作为一个bug提交给苹果,因为主包是不可写的,但没有出现“拒绝访问”错误。系统显然是在撒谎。谢谢,现在它可以工作了,但是如何在Documents目录中创建文件夹呢?