从文本文件导入 arrayWithObjects 的 NSString 对象

发布于 2025-01-07 23:10:31 字数 368 浏览 6 评论 0 原文

我是开发新手,我正在尝试创建一个简单的词典应用程序供个人在 iPhone 上使用。对于上下文,我的问题是 这个问题的后续问题 你们中的一些人昨天很友善地回答了。

是否可以创建一个方法:

  1. 读取文本文件
  2. 将文件中的文本字符串转换为 NSStrings
  3. 使用 NSStrings 填充 NSArray 以创建 arrayWithObjects

任何帮助或示例代码将不胜感激!

I'm new to development and Im trying to create a simple dictionary app for personal use on the iPhone. For context, my question is a follow up to this one that some of you were kind enough to answer yesterday.

Is it possible to create a method that:

  1. Reads a text file
  2. Converts text strings in the file to NSStrings
  3. Populates an NSArray with the NSStrings to create an arrayWithObjects

Any help or example code would be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟沫凡尘 2025-01-14 23:10:31

将文本文件格式化为有用的数据结构并解析它将是最难的部分。但是,只要您的 NSArray 或 NSDictionary 只包含 plist 可序列化对象,例如 NSString 就可以考虑使用简单的序列化。

您可以使用它来保存:(其中 array 是要保存的数组,path 是文件路径的 NSString)

[array writeToFile:path atomically:YES];

并且可以从文件中加载:

NSArray *array = [NSArray arrayWithContentsOfFile:path];

您还可以使用 NSJSONSerialization 来创建和结构化字符串并通过更通用的方式保存它。

Formatting the text file to be a useful data structure and parsing it would be the hardest part. But, as long as your NSArray or NSDictionary contain only plist serializable objects, like NSStrings consider just using simple serialization.

You could use this to save: (Where array is the array to save and path is an NSString of the file path)

[array writeToFile:path atomically:YES];

And this to load from the file:

NSArray *array = [NSArray arrayWithContentsOfFile:path];

You could also use NSJSONSerialization to create and structured string and save it through more general means.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文