Objective-C 创建一个带有字符串的文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1820204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:33:55  来源:igfitidea点击:

Objective-C creating a text file with a string

objective-ccocoansstringsave

提问by ACV

I'm trying to create a text file with the contents of a string to my desktop. I'm not sure if I'm doing it right, I don't get errors but it doesn't work either...

我正在尝试创建一个包含字符串内容的文本文件到我的桌面。我不确定我是否做得对,我没有收到错误,但它也不起作用......

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES);
NSString *desktopDirectory=[paths objectAtIndex:0];
NSString *filename = [desktopDirectory stringByAppendingString: @"file.txt"];
[myString writeToFile:filename atomically:YES encoding: NSUTF8StringEncoding error: NULL];

回答by megha

//Method writes a string to a text file
-(void) writeToTextFile{
    //get the documents directory:
    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 *fileName = [NSString stringWithFormat:@"%@/textfile.txt", 
                                                  documentsDirectory];
    //create content - four lines of text
    NSString *content = @"One\nTwo\nThree\nFour\nFive";
    //save content to the documents directory
    [content writeToFile:fileName 
                     atomically:NO 
                           encoding:NSStringEncodingConversionAllowLossy 
                                  error:nil];

}

回答by Joshua Nozzi

You don't know if you're getting any errors because you're ignoring the returned YES/NO value of the -writeToFile:... method, and giving it no error pointer into which to record any possible failure. If the method returns NO, you'd check (and handle or present) the error to see what went wrong.

您不知道是否遇到任何错误,因为您忽略了 -writeToFile:... 方法返回的 YES/NO 值,并且没有提供错误指针来记录任何可能的失败。如果该方法返回 NO,您将检查(并处理或呈现)错误以查看出了什么问题。

At a guess, the failure is due to the path you constructed. Try -stringByAppendingPathComponent: instead of -stringByAppendingString: ... this and its related methods properly handle paths.

猜测,失败是由于您构建的路径。尝试 -stringByAppendingPathComponent: 而不是 -stringByAppendingString: ... 这个及其相关方法正确处理路径。

The file probably isactually being created (ie, you might not be getting any errors after all). My guess is the file is created somewhere like "~/Desktopfile.txt" since your use of -stringByAppendingString: doesn't consider the string as slash-separated path. Check your home folder - I'll bet the file's there.

该文件可能实际被创建(比如,你可能不会毕竟得到任何错误)。我的猜测是该文件是在“~/Desktopfile.txt”之类的地方创建的,因为您使用 -stringByAppendingString: 不会将该字符串视为斜线分隔的路径。检查您的主文件夹 - 我敢打赌文件在那里。

回答by Eman yalpsid

the problem is that the desktop directory string ends in nothing (no /). Check this out (on an iPhone) by using UIAlertview.

问题是桌面目录字符串以无结尾(没有/)。使用 UIAlertview 检查(在 iPhone 上)。