在iOS中读取文本文件 [英] Reading in a text file in iOS
问题描述
如何将文本文件读入textView?我有许多不同的文本文件,用户选择显示哪个文本文件,因此这就是为什么我不仅仅在其中有文件名的原因.我真的很困惑为什么这行不通.我在Apple Developer网站上有关从文件读取到字符串的部分中找到了它.
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;
推荐答案
-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:
textView.text = textFile;
这篇关于在iOS中读取文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!