Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

99miles

macrumors member
Original poster
Oct 10, 2008
50
0
Hi -
What's the best way to load/create a large array of strings? When in a text file, the file is about 2 MB.

The contents will never change so I could keep it in my source (but then my app takes forever to compile), I can store it in a text file and load it, then create the array by using newline as a delimiter but [[[NSString stringWithContentsOfFile:filePath] componentsSeparatedByString:mad:"\r\n"] takes about 5 seconds.

Is there a faster/better way?
Thanks
 

North Bronson

macrumors 6502
Oct 31, 2007
395
1
San José
Hi -
What's the best way to load/create a large array of strings? When in a text file, the file is about 2 MB.

The contents will never change so I could keep it in my source (but then my app takes forever to compile), I can store it in a text file and load it, then create the array by using newline as a delimiter but [[[NSString stringWithContentsOfFile:filePath] componentsSeparatedByString:mad:"\r\n"] takes about 5 seconds.

Is there a faster/better way?
Thanks

Why don't you try creating an XML file? Then you just need:

NSArray *newMyArray = [[NSArray alloc] initWithContentsOfFile: myXMLFile];

I'm pretty sure there is a method to take the TXT file that you have now and turn it into an XML for you.
 

bnut

macrumors newbie
Nov 16, 2008
28
0
In general if you need speed go lower level, there are small overheads with Objective C methods. I may be wrong, but I imagine your method call will allocate the NSString for the entire file, perhaps do a representation change on initialisation (this I assume). Then it will parse it again and split it into the substrings, which will have to be allocated as well. If you have an already allocated buffer for a line, then use a scanner to read each line, then create the NSString from the data with the appropriate encoding, I would assume it to be faster... however probably not future proof or particularly nice OO. :p I'd expect 2MB to take a while anyway.
 

jnic

macrumors 6502a
Oct 24, 2008
567
0
Cambridge
Seconding bnut; for speed you really should compile your data in an array. If you keep it in a static method in its own class it shouldn't need to be compiled more than once (Xcode will cache the compiled code) so won't cause much of a slow down when compiling.
 

nomar383

macrumors 65816
Jan 29, 2008
1,310
0
Rexburg, ID
I'm in the same type of situation. I have about 2-5MBish worth of static text that will need to be stored in some fashion. What is the best way? An SQL database, XML, plist? Should I really just hardcode it into its own method (in an array)? I haven't been able to find much information on this type of thing online
 

jnic

macrumors 6502a
Oct 24, 2008
567
0
Cambridge
I'm in the same type of situation. I have about 2-5MBish worth of static text that will need to be stored in some fashion. What is the best way? An SQL database, XML, plist? Should I really just hardcode it into its own method (in an array)? I haven't been able to find much information on this type of thing online

Depends how you want to access it, but for speed your best bet is going to be either SQL or hardcoding.
 

nomar383

macrumors 65816
Jan 29, 2008
1,310
0
Rexburg, ID
Depends how you want to access it, but for speed your best bet is going to be either SQL or hardcoding.

Hard coding is very very tempting as I don't know SQL that well. Will it affect the overall load time of the app to do it this way?
 

jnic

macrumors 6502a
Oct 24, 2008
567
0
Cambridge
Hard coding is very very tempting as I don't know SQL that well. Will it affect the overall load time of the app to do it this way?

Load time for hard coded will be far faster than XML/plist since there's no processing to do to get the data into a useful state :)
 

boyplunder

macrumors regular
Sep 8, 2008
165
0
UK
data tables

I had an idea for a few apps, but needed to know what the best structure was for what I wanted to do. As a newbie, I had a long journey to work things out, but am quite methodic in my approach, and didn't mind too much how long it took me. So looked at SQLite, plist and resident array solutions I found in some terrific tutorials on the web.

Hard coding the data into an array in the app was the fastest and simplest method, but was a bit of a pain managing the amount of data entries I was working up to. A plist made things much easier to manage, but the added setup and table management code made a fair loading difference when you have over 1000 titles and related details. The same can be said for a SQLite db. [Incidentally, I use SQLite manager in Firefox.]

So, the conclusion I came to was for apps with just fifty or less entries, or picker/selection lists, I would use a resident array or maybe a plist, for anything more than that I would use SQLite.

Hope my ramble helps.

Boyplunder
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.