如何从文本字段读取用户输入文本并将其写入 Xcode 中的文本文件?
有没有办法将从文本字段读取的 string 写入 .txt 文件?
NSString *input = [textfield text];
NSString *path = @"myText.txt";
[input writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:NULL];
Is there a way to write a string , which is read from text field, to a .txt file?
NSString *input = [textfield text];
NSString *path = @"myText.txt";
[input writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:NULL];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,最终就是这样;但是为了从开始到结束,您还需要执行许多其他步骤(您需要将文本字段连接到 IBOutlet 或以其他方式可访问;myText.txt 写入的路径需要是可写的,因此您可能需要有一个比文件名更长、更精确的路径;您可能还需要发送一个可以设置的实际错误参数,以便您可以查看 writeToFile 调用第一次失败时返回的错误你运行这个几次 代码)。
Yes, ultimately that is the way; but there's a number of other steps you do in order to get from start to finish (you need to have textfield connected to an IBOutlet or in some other way accessible; the path where myText.txt is writing to needs to be writable and therefore you'd probably need to have a longer, more precise path than just the filename; you'd probably need to also send in an actual error parameter that could be set so you could look at the errors being returned when the writeToFile call fails the first few times you run this code).
您需要定义一个路径,基本上是保存文件的位置。
该代码将找到 Documents 文件夹
这是我用来读取和写入文件的代码。
You will need to define a path, basically where to save the file.
This code will find the Documents folder
Here is the code I use to read and write files.