2

I'm new to development and Im trying to create a simple dictionary app for personal use on the iPhone. For context, my question is a follow up to this one that some of you were kind enough to answer yesterday.

Is it possible to create a method that:

  1. Reads a text file
  2. Converts text strings in the file to NSStrings
  3. Populates an NSArray with the NSStrings to create an arrayWithObjects

Any help or example code would be greatly appreciated!

Community
  • 1
  • 1
Orpheus Mercury
  • 1,617
  • 2
  • 15
  • 30
  • I'm new here and I noticed that I received a "-1" and I don't understand why. Is my question badly flawed? I just want to know if way I'm asking is possible so that I can point my research in the right direction. Thanks. – Orpheus Mercury Feb 21 '12 at 20:47
  • I just stumbled upon the stringWithContentsOfFile: method. I guess that's what I'd use to read the file. Still unsure how I'd create an arrayWithObjects from the resulting NSStrings. – Orpheus Mercury Feb 21 '12 at 20:55
  • 2
    I can't know what the people who down voted your question were thinking, but I see 3 issues that could trigger that behavior: 1) You have structured your question very nicely, so nicely in fact that there are really three questions in one 2) Asking for example code is generally frowned upon 3) The community tends to appreciate signs that you have made some effort yourself before asking a question. And with those remarks, I think yours is actually a relevant question. – Monolo Feb 21 '12 at 20:56
  • Understood. Thanks for the tips @Monolo. I'll try to work this out myself. – Orpheus Mercury Feb 21 '12 at 21:03

1 Answers1

2

Formatting the text file to be a useful data structure and parsing it would be the hardest part. But, as long as your NSArray or NSDictionary contain only plist serializable objects, like NSStrings consider just using simple serialization.

You could use this to save: (Where array is the array to save and path is an NSString of the file path)

[array writeToFile:path atomically:YES];

And this to load from the file:

NSArray *array = [NSArray arrayWithContentsOfFile:path];

You could also use NSJSONSerialization to create and structured string and save it through more general means.

NJones
  • 27,139
  • 8
  • 70
  • 88