Orpheus Mercury
Orpheus Mercury

Reputation: 1627

Import NSString objects for an arrayWithObjects from text file

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!

Upvotes: 2

Views: 214

Answers (1)

NJones
NJones

Reputation: 27147

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.

Upvotes: 2

Related Questions