ACV*_*ACV 16 cocoa objective-c save nsstring
我正在尝试使用字符串的内容创建一个文本文件到我的桌面.我不确定我是否做得对,我没有得到错误,但它也不起作用......
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];
Run Code Online (Sandbox Code Playgroud)
小智 46
//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];
}
Run Code Online (Sandbox Code Playgroud)
Jos*_*zzi 13
您不知道是否收到任何错误,因为您忽略了-writeToFile:...方法返回的YES/NO值,并且没有给它记录任何可能的失败的错误指针.如果方法返回NO,则检查(并处理或出现)错误以查看错误.
猜测,失败是由于你构建的路径.尝试-stringByAppendingPathComponent:而不是-stringByAppendingString:...这及其相关方法正确处理路径.
该文件可能是实际被创建(比如,你可能不会毕竟得到任何错误).我的猜测是文件是在"〜/ Desktopfile.txt"之类的地方创建的,因为你使用-stringByAppendingString:不会将字符串视为斜杠分隔的路径.检查你的主文件夹 - 我打赌文件在那里.
归档时间: |
|
查看次数: |
27504 次 |
最近记录: |