0

I'm currently working on a project that involves a lot of text file reading. I need to get the contents of the files into NSStrings so I can go and manipulate them further. What I'm using:

NSString *file = [[NSBundle mainBundle] pathForResource:fileName ofType:@"txt"];
NSString *fileContents = [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil];

It works well enough, but as my project grows so does the number of text files. I'm starting to think there should be a more efficient way of searching through the text files included in the project.

In many cases, I know exactly what file I want to search. I have the files organized into group folders based on when they need to be read. Is there any way to narrow the range of the initial file search? It seems like searching through the entire application bundle every time is pointless when I know where the files I need are on a more specific level.

Is there some way to search through groups as opposed to bundles? Can I somehow exclude certain files from the search? From what I can tell, defining custom bundles for this context would not be an appropriate use of the NSBundle functionality.

Cœur
  • 37,241
  • 25
  • 195
  • 267
flickMik
  • 27
  • 3
  • 1
    To really increase the speed, consider adding all of the strings into core data or a sql database so that you can index them and do really fast lookups (by comparison). – lnafziger May 05 '12 at 06:02

2 Answers2

1

Have you looked at the method -[NSBundle pathForResource:ofType:inDirectory:]?

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • Oh wow, that's perfect. Not sure how I missed that one as I had looked at the NSBundle documentation. Not close enough it seems! Thanks a million. – flickMik May 05 '12 at 05:32
0

I had almost similar kind of situation, for more optimization, i had kept all resource file inside dictionary with some key ,

you may try following

1 -- in our application you may have some kind of pattern or where you could find major group,

2 -- have multiple dictionary each for each group, and inside group, you could store file name,

3 -- As ken suggested to go with the -[NSBundle pathForResource:ofType:inDirectory:]

Amitg2k12
  • 3,765
  • 10
  • 48
  • 97