views:

1229

answers:

4

I have several arrays of arrays or arrays of dicts that I would like to store in my iPhone app. This lists are static and won't be modified by the app or users. Occasionally they may be displayed but more likely they'll be iterated over and compared to some input value. Would the best way to store these arrays be a CoreData/SQLite data store, in a header file, or something I'm not thinking of? I could see making a class that only has these arrays stored in them for access, but I'm not sure if that's the best route to take.

+5  A: 

Use a property list file. Load it with NSDictionary +dictionaryWithContentsofFile:.

Chris Lundie
While I would agree with you on the Mac, I disagree on the iPhone. Doing IO is basically a waste of time if they are just static. If the dataset is too large to fit in memory it should be in SQLite but the OP didn't mention anything about the dataset being particularly big.
Colin Barrett
IO is IO... If you compile it into your app you will be performing the IO, it will just be fragmented into your your apps TEXT section and demand loaded by the file pager. If you place it in a file you can control the locality, if your data is the apps TEXT you have less control over it.
Louis Gerbarg
+1  A: 

Depending on how often you want to modify or localize the items, and your lookup time requirements, a static array may also be the way to go. For constant data, however, SQLite is probably not the route to take, unless you have complex query requirements (as opposed to just by-index).

Ben Gottlieb
+3  A: 

I would do the following:

@implementation DataSource
+ (NSArray *)someData
{
  static NSArray *data = nil;
  if (!data) {
    data = [[NSArray arrayWithObjects:..., nil] retain];
  }
  return data;
}
@end

Additional arrays or arrays of dicts would be added as class methods on that class.

Colin Barrett
+3  A: 

I would go for the plist approach.

wisequark