在 iOS 中读取文本文件
如何将文本文件读入textView?我有许多不同的文本文件,用户选择显示哪个,所以这就是为什么我不只是在其中有一个文件名。我真的很困惑为什么这不起作用。我在 Apple 开发者网站上有关从文件读取到字符串的部分中找到了它。
NSString *path = [[NSBundle mainBundle] pathForResource:@"%@" ofType:@"txt"];
textFile = [[NSString alloc] initWithContentsofFile:path];
textView.text = textFile;
How do I read in a text file into a textView? I have many different text files and the user selects which one is displayed, so that is why I don't just have a file name in there. I'm really confused about why this doesn't work. I found it right on the Apple Developer site in the section about reading from files to strings.
NSString *path = [[NSBundle mainBundle] pathForResource:@"%@" ofType:@"txt"];
textFile = [[NSString alloc] initWithContentsofFile:path];
textView.text = textFile;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
-pathForResource:ofType:
不接受格式字符串;将@"%@"
替换为包含文件基本名称的字符串(即文件名减去 .txt 扩展名)。第二行和第三行也不匹配:您的意思可能是:
-pathForResource:ofType:
doesn't accept a format string; replace the@"%@"
with a string containing the base name of the file (i.e. the filename minus the .txt extention).You've also got a mismatch on the second and third lines: you probably meant: