我是 objective-c 的新手,我在 C、C++ 方面有不错的经验。我将如何阅读包含 4 行单词的文本文件。我将不得不分别阅读每一行并将每一行存储在各自的 NSString 中。即,文件中包含 4 行文本的 4 个字符串。我对 Obj-C 中的方法还是陌生的,所以非常感谢您的帮助!
PS:我已经开始阅读 CoreData,但我怀疑这是否与此有关。
最佳答案
首先获取app bundle中文件的路径:
NSString *filename = @"textfile.txt";
NSString *path = [[NSBundle mainBundle] pathForResource:[filename stringByDeletingPathExtension] ofType:[filename pathExtension]];
接下来将文件内容加载到一个字符串中,并将它们拆分为一个字符串数组:
NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
NSArray *lines = [text componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
嘿,转眼间。
关于ios - (Objective-C) 从资源中的 txt 文件中读取多个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20270553/