xcode 将 NSString 写入文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10352665/
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-15 00:07:09  来源:igfitidea点击:

write NSString to file

objective-ciosxcodefile

提问by Niels S?nderb?k

I'm trying to write a string to a file and store that in the applicationSupport folder within my app. But my NSLog() statement doesn't log anything in the debugger. I have tried to go through it with the debugger, and i can see that the content is nil, so i'm guessing that's why it's not outputting anything, but i don't know why it's set to nil. Can anybody see my mistake?

我正在尝试将一个字符串写入文件并将其存储在我的应用程序内的 applicationSupport 文件夹中。但是我的 NSLog() 语句没有在调试器中记录任何内容。我试图用调试器来完成它,我可以看到内容为零,所以我猜这就是它不输出任何东西的原因,但我不知道为什么它被设置为零。有人能看到我的错误吗?

 NSString *document = [[[[[[[[description stringByAppendingString:@" "]stringByAppendingString:focus]stringByAppendingString:@" "]stringByAppendingString:level]stringByAppendingString:@" "]stringByAppendingString:equipment]stringByAppendingString:@" "]stringByAppendingString:waterDepth];
//NSLog(@"%@", document);

//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *supportDirectory = [paths objectAtIndex:0];

//filename
NSString *filename = [NSString stringWithFormat:[_nameTextField.text stringByAppendingString:@".txt"], supportDirectory];

[document writeToFile:filename atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];

NSString *content = [[NSString alloc]initWithContentsOfFile:filename usedEncoding:nil error:nil];
NSLog(@"%@", content);

回答by sch

filenameis wrong. It should have the following format: "directory/file.extension". You can use the methods stringByAppendingPathComponentand stringByAppendingPathExtensionto construct such a string.

filename是错的。它应该具有以下格式:"directory/file.extension". 您可以使用的方法stringByAppendingPathComponentstringByAppendingPathExtension构建这样的字符串。

NSString *filename = [supportDirectory stringByAppendingPathComponent:[_nameTextField.text stringByAppendingPathExtension:@"txt"]];


Also, as a side note, the first line should be rewritten using stringWithFormatlike this:

另外,作为旁注,第一行应该stringWithFormat像这样重写:

NSString *document = [NSString stringWithFormat:@"%@ %@ %@ %@ %@", description, focus, level, equipment, waterDepth];

回答by Alex

You have wrong initialize filename. It should be:

你有错误的初始化文件名​​。它应该是:

  NSString *filename = [NSString stringWithFormat:@"%@/%@", supportDirectory,[_nameTextField.text stringByAppendingString:@".txt"]];

it will works! Good luck:)

它会起作用!祝你好运:)

回答by Lefteris

The

NSString *filename = [NSString stringWithFormat:[_nameTextField.text stringByAppendingString:@".txt"], supportDirectory];

is wrong. It should be:

是错的。它应该是:

NSString *filename = [NSString stringWithFormat:"%@%@",[_nameTextField.text stringByAppendingString:@".txt"], supportDirectory];