0

I have the need for a 2-dimensional look-up table for my app. I wanted to do this as simply as possible, so I just made a series of arrays, and then will just call the appropriate array and position within that array whenever needed. However, this takes about 500 arrays... here is a code snippet:

 NSArray *key_1 = [[NSArray alloc] initWithObjects:@"1",@"0.5038",@"0.8054",@"4.51",nil];
 NSArray *key_2 = [[NSArray alloc] initWithObjects:@"1",@"0.4869",@"0.8009",@"4.7",nil];
 NSArray *key_3 = [[NSArray alloc] initWithObjects:@"1",@"0.4708",@"0.7967",@"4.9",nil];
 NSArray *key_4 = [[NSArray alloc] initWithObjects:@"1",@"0.4554",@"0.7926",@"5.09",nil];
 NSArray *key_5 = [[NSArray alloc] initWithObjects:@"1",@"0.4407",@"0.7889",@"5.27",nil];
 NSArray *key_6 = [[NSArray alloc] initWithObjects:@"1",@"0.426",@"0.7853",@"5.46",nil];
 NSArray *key_7 = [[NSArray alloc] initWithObjects:@"1",@"0.4133",@"0.7819",@"5.65",nil];

However, I have two concerns. First, if these 500 array declarations cause a momentary hang in the app, where should I put this code so that it executes while still on the app's splash screen when first starting up? Would that be in viewWillAppear?

Second (and this is probably more of a newb question), if I place these array declarations in viewWillAppear or some other method in my FirstViewController.m, how can I make them accessible to methods in my SecondViewController.m?

Thanks!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
jake9115
  • 3,964
  • 12
  • 49
  • 78
  • 5
    Try using plist files for this. Write your data in that in dictionary format (Key-Value). And fetch only required data using specific key. Hope this helps: http://stackoverflow.com/questions/9335475/iphone-read-write-plist-file – Mrunal Jul 11 '13 at 14:23
  • Are these float values wrapped in a NSString? This looks like a job for good old plain c arrays. With floats. – Matthias Bauch Jul 11 '13 at 16:06

1 Answers1

0

So you have two options

First: You can load these arrays on different thread (not on main thread). You can use GCD to create a new thread and pass a these array as blocks to work on. By doing this you can solve your first problem "momentary hang".

Second: As Mrunal mentioned "Try using plist files for this. Write your data in that in dictionary format (Key-Value). And fetch only required data using specific key."

I like to option number two but if you have dynamic array please use option number one.

AAV
  • 3,785
  • 8
  • 32
  • 59
  • before putting stuff on a secondary thread I would profile how much time the task actual takes. Instantiating 500 arrays with 4 NSStrings literals (!) each will probably take less than the blink of an eye. – Matthias Bauch Jul 11 '13 at 16:06