Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.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 8在模拟器中写入文件_Ios_Objective C - Fatal编程技术网

iOS 8在模拟器中写入文件

iOS 8在模拟器中写入文件,ios,objective-c,Ios,Objective C,我正在尝试使用以下代码将一个简单字符串写入文件: NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *fileName = [NSString stringWithFormat:@"%@/text

我正在尝试使用以下代码将一个简单字符串写入文件:

    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *fileName = [NSString stringWithFormat:@"%@/textfile.txt",
                          documentsDirectory];
    NSString *content = @"Test Content";
    NSError *error = nil;
    [content writeToFile:fileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:&error];

    NSLog(@"Error %@ %@", fileName, error);
但文件未写入,错误为空


我正在iOS模拟器中运行代码,希望将文件写入如下位置:Users/user/Library/Developer/CoreSimulator/Devices/85DDC17D-A771-40D9-99BE-71FF6B60D2DF/data/Containers/data/Application/BD30A945-25DB-4ECE-B7F7-E9C20C6691C7/Documents/textfile.txt(NSLog中的文件名路径).

请检查以下代码:

   NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *filePath = [[pathsobjectAtIndex:0]stringByAppendingPathComponent:@"textfile.txt"];
   NSString *temp = [[NSString alloc] initWithString:@"Test Content"];
   [temp writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];

使用URL是一种比较可靠的方法,因此我会稍微重复您的想法,例如:

NSURL *_documentFolder = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *_fileURL = [_documentFolder URLByAppendingPathComponent:@"textFile.txt"];
NSError *_error = nil;
BOOL _success = [@"hello!" writeToURL:_fileURL atomically:TRUE encoding:NSUTF8StringEncoding error:&_error];
这在模拟器和真实设备上也能完美工作,文件
textFile.txt
将包含内容
hello



注意:如果
\u success
值为
FALSE
,您可以通过
\u错误
获取有关原因的更多信息,即使您只是将其记录到调试控制台。在我的示例中,我没有隔离
NSString
,但是您可以用自己的实例替换它。

正如@rckoenes指出的,这是我代码其余部分中的一个错误。我有一个删除“文档”中所有文件的函数,我只是不记得我有这个函数,因为代码的长度(>5000行)很难找到。谢谢大家!

writeToFile:atomicaly:encoding:
”返回一个BOOL值,以指示写入的成功或失败。在您的情况下,它返回真还是假?它返回真。但是文件还没有写。我现在已经创建了一个测试项目,并执行了相同的代码,文件也被写入了。所以,也许这个项目还有一些事情要做?我已经将上面的代码复制粘贴到了一个新的应用程序中,它就可以正常工作了。你确定你使用的是上面的正确代码吗?还可以创建文件名变量,如:
NSString*fileName=[DocumentsDirectoryStringByAppendingPathComponent:@“textfile.txt”]你怎么知道文件没有被写入?是的,我使用的是完全相同的代码,正如我前面所说的,代码在一个空项目中对我有效,但在我当前的项目中不起作用,该项目最初是为iOS 7创建的。不要仅仅发布代码,解释不同之处以及原因!可爱的。。。一个天才没有留下任何评论就被否决了。。。通过强迫那些白痴留下一些评论,这个该死的系统最终会被改造吗?这对社区没有帮助——因为就我个人而言,我不知道应该如何改进答案。如果有人有,请给我一个提示。谢谢