开发者

iOS should I use NSMutableArray or NSMutableDictionary?

开发者 https://www.devze.com 2023-02-22 12:53 出处:网络
I have a c开发者_开发问答lass called Person The person class stores 6 bits of data System generated ID (integer)

I have a c开发者_开发问答lass 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

  1. Searching
  2. Adding new items
  3. Removing items

Eventually I'll be writing my class out to disk and reading it when the application launches.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消