在 plist 中搜索整数
在 iOS 应用程序中,我从用户处获取四位数代码,并在 TextView 中为他们提供相应的字符串。我的问题是,因为我正在检查用户可能输入的大约一千种代码,所以在没有大量 if 或 switch 语句的情况下给出结果的有效方法是什么?比如,使用 plist、txt 文件,甚至数据库......提前致谢
In an iOS app, I take a four digit code from the user and give them a corresponding string in a TextView. My question is, because there are about a thousand possible codes the user would enter that I am checking for, what is an efficient way to give a result without having a huge if or switch statement? Like, using a plist, txt file, or even database.... Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
选择plist、文本文件还是数据库只是存储的问题,而不是搜索的问题。就我个人而言,我只会使用 JSON 文件,因为它得到了人脑和软件的良好支持。对于搜索,只需将它们放入 NSDictionary 中并进行查找即可。除非您的项目非常大,否则 1000 个项目实际上并不是一个大数据集,即使在内存有限的 iPhone 上也是如此。即使每个项目都是 1 KB(听起来比您描述的数据集大很多),您看到的整个集合还不到 1 MB。
如果字符串很长,则将长文本存储在文件中,并将文件 URL 而不是整个字符串存储在查找表中。 IIRC,一个 URL 平均约为 100 个字节,一个 NSNumber 约为 8 个字节,因此整个数据集大约需要 108 KB。
The decision of plist, text file or database is just a matter of storage, not search. Personally, I would just use a JSON file, since it's reasonably well-supported both by the human brain and by software. For searching, just put them in an NSDictionary and do a lookup on that. Unless your items are very big, 1000 items is not really a large dataset, even on a memory-constrained iPhone. Even if each item is 1 KB (which sounds a lot larger than the dataset you're describing), you're looking at less than a megabyte for the whole set.
If the strings happen to be long, then store the long text in a file and store the file URL in your lookup table instead of the whole string. IIRC, a URL is about 100 bytes on average, and an NSNumber is about 8, so you'd be looking at about 108 KB for the entire dataset.
考虑到可能的代码数量,我建议 Core Data< /a>.或者,您可以直接使用SQLite。您可以使用plist,但我担心当您添加、删除和更新代码时它很快就会变得难以管理。
Given the number of possible codes I would recommend Core Data. Alternatively you can use SQLite directly. You could use a plist, but I fear it would quickly become unmanageable as you add, remove, and update codes.