r/simpleios • • Sep 28 '11

[Question] NSArray Structure for UITableView?

Hi guys, I have a question for someone who has experience working with NSArrays and NSDictionaries. Here's what I'm trying to do:

My app will display location data in a UITableView. The first view will be 'tracks', essentially a collection of waypoints that the user can name. When you click on this track, it will show you a list of individual waypoints in another table view. When the user clicks on one of these it takes them to a detail view with coordinates, a map, and some other options.

My question is how do I create a structure of arrays or dictionaries that allow me to store all of this data in a single array, and access it efficiently? I've found that dictionaries are hard to traverse (at least for me), and I've also read arrays are better for table views. Any tips?

4 Upvotes

10 comments sorted by

View all comments

Show parent comment

2

u/schmeebis [M] 📱 Sep 28 '11

No problem, glad I could help out. And since NSArray and CLLocation both conform to NSCoding, you can do things like saving them to disk and loading them from disk later, very easily.

Saving:

NSString* docFolder = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* archivePath = [docFolder stringByAppendingPathComponent:@"LocationHistory.db"];
BOOL success = [NSKeyedArchiver archiveRootObject:locationHistoryArray toFile:archivePath];

Loading:

NSString* docFolder = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* archivePath = [docFolder stringByAppendingPathComponent:@"LocationHistory.db"];
NSMutableArray* locationHistoryArray = [NSKeyedUnarchiver unarchiveObjectWithFile:archivePath];