iOS 我应该使用 NSMutableArray 还是 NSMutableDictionary?
我有一个名为 Person 的类
person 类存储 6 位数据
系统生成的 ID(整数) 姓名 年龄 网站网址 数字(浮点数) Number (float)
我想存储这个对象并稍后访问它。
将其存储在 NSDictionary (或 NSMutableDictionary)中或使用 NSArray (或 NSMutableArray) 是一个好主意
我将执行的任务类型是
- 搜索
- 添加新项目
- 删除项目
最终我会将我的类写入磁盘并当应用程序启动时读取它。
I have a class called Person
The person class stores 6 bits of data
System generated ID (integer)
Name
Age
Website URL
Number (float)
Number (float)
I want to store this object and access it later.
Would be be a good idea storing it in an NSDictionary (or NSMutableDictionary) or using an NSArray (or NSMutableArray)
The types of tasks I will be performing are
- Searching
- Adding new items
- Removing items
Eventually I'll be writing my class out to disk and reading it when the application launches.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您打算存储多少个 Person 对象?您打算通过哪些属性来搜索它们?
如果您希望能够通过这些属性中的任何一个来查找一个人,您将需要六个字典才能以完整的 O(1) 字典效率来完成此操作。使用数组,您通常可以获得 O(n),或者您可以按一个键排序,并为该键获得 O(logN),为其他键获得 O(n)。
为了简单起见,除非您有大量的 Person 对象,否则我建议使用数组。
How many Person objects do you intend to store? Which attributes do you intend to search for them by?
If you want to be able to look up a person by any of those attributes, you would need six dictionaries to do so at full O(1) dictionary efficiency. With an array you could get O(n) in general, or you could sort by one key and get O(logN) for that key and O(n) for the others.
In the interests of simplicity, unless you have a very large number of Person objects, I would suggest using an array.
我会使用一个数组,就像字典一样,你必须决定使用哪个键,并且不能对重复的条目重复使用相同的键。
I would use an array, as for a dictionary you have to make up your mind what key to use, and you cannot reuse the same key for duplicate entries.