将 .plist 数组读入 NSArray 时过滤元素
第一篇文章 - 希望我做得对!
我有一个文件 lexicon.plist,其中包含大约 250K 单词的数组。我想将长度为“n”的所有单词加载到 NSArray 中。
我知道 NSArray 实例方法:
- (id)initWithContentsOfFile:(NSString *)aPath
但我没有看到任何方法可以干预将文件读入 NSArray 的过程。我能看到的唯一解决方案是首先将整个词典加载到一个 NSArray 中,然后在循环中运行它,选择长度为“n”的元素。
我对 Cocoa 很陌生,但我遇到过一些执行某种迭代任务的方法,这些方法接受在每次迭代时调用的代码“块”。我想知道 initWithContentsOfFile 的这种功能变体是否可能存在,或者我如何从 .plist 文件迭代读取数组并过滤我感兴趣的元素。
[如果您想知道这是否可能是一种情况过早优化 - 是的;-) 但我仍然想知道。]
First post - hope I'm doing it right!
I have a file, lexicon.plist, containing an array of about 250K words. I want to load all words of length 'n' into an NSArray.
I know about the NSArray instance method:
- (id)initWithContentsOfFile:(NSString *)aPath
but I don't see any way to intervene in the process of reading the file into the NSArray. The only solution I can see is to first load the entire lexicon into one NSArray, and then run through it in a loop selecting the elements of length 'n'.
I'm very new at Cocoa, but I have come across some methods that perform some sort of iterative task, that accept a "block" of code that is invoked at each iteration. I was wondering if such a functional variant of initWithContentsOfFile might exist, or how else I might iteratively read an array from a .plist file and filter the elements I'm interested in.
[And if you're wondering if this might be a case of premature optimization - it is ;-) But I'd still like to know.]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
.plist
文件基本上是 XML 文件,因此您可以在其上使用 NSXMLParser 并过滤掉感兴趣的元素。.plist
files are basically XML files so you can use an NSXMLParser on it and filter out the elements of interest.例如,如果要加载已保存数据的筛选选择,则应使用 SQL 存储库。
普通文件只能完全加载到内存中。
If you want to load a filtered selection of a saved data, you should use a SQL repository using SQLite, for example.
Plain files can only be fully loaded in memory.