ios - (Objective-C) 从资源中的 txt 文件中读取多个单词

标签 ios objective-c text nsstring

我是 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/

相关文章:

ios - 如何在 Windows 上使用 SwiftUI 制作 iOS 应用程序?

ios - xcode 5 水平 ScrollView 不工作 (iOS7)

objective-c - iPhone/iPad itunes 库读取权限?

linux - 标记两个文件中的差异

python - 皮格莱特 : using on_resize() event causes my text to disapear

ios - 更新到 Xcode 5.1 后出现超过 130 个错误

ios - IPad加速度计是否按重力归一化

ios - 在 Swift 中对 @objc 类中的变量进行延迟初始化

ios - 无法返回 - popViewController 动画无法正常工作,向后滑动也无法正常工作

android - 如何将 TypefaceSpan 或 StyleSpan 与自定义字体一起使用?