Question

Yes, I've looked through the numerous questions similar to this that were asked already; however, I still did not quite understand what was going on or at least I couldn't figure out how to do it in my application.

So basically my "practice" app has a main view controller that displays two text labels. These text labels are populated by an NSObject class that holds a large array. The user inputs the number in the array they want to go to, and the string at that index in the array becomes the text in one label, while the user-input number becomes the text in the other label. The user can also "favorite" a string from the array by pressing a button. When this happens that string and it's index number are added to two separate mutable arrays that reside in the main view controller. Then, when the user wants to see all of their "favorites", they can swipe up to (modally) bring up a uitableview that has all of the user's "favorite strings". This is achieved by passing the data (2 mutable arrays) from the main view controller to the uitableviewcontroller using a navigation controller/segue. At this point, there are 2 mutable arrays in the uitableviewcontroller which contain the strings and index numbers from the main view controller. This data is then passed back to the main view controller using delegate/protocol to set the text (in the row that the user tapped on the uitableview) in the label on the main view controller.

I have no problem building the app; everything works perfectly. But I have no idea how to save the data so that when the user quits the application and reopens it they can still see the table view with all of the strings that they've favorited. Also, I'm just assuming that in my case, I should use NSCoding; if, however, I need to use Core Data or anything else, then let me know. If anyone can help me out, I'd really really appreciate it. Very detailed explanations please, and sample code if possible.

Was it helpful?

Solution 3

It turns out I was doing it right the whole time. I used NSUserDefaults to save the arrays provided that they weren't empty, then I set the arrays to their NSUserDefaults if their key existed. My only problem was trying to figure out where to do the saving and loading. At first, I was doing it in viewWillAppear & viewWillDisappear, but after taking a step back and really understanding what NSUserDefaults did, I did it in viewWillDisappear & viewDidLoad. Thanks for all the help anyhow. Cheers.

OTHER TIPS

You can use one of the following way to acheive it

  1. Core data - If you are handling large number of records then go o for it.but as per my understanding it is more than necessary for your solution.
  2. NSUserDefauls - If your favourites are restricted to just hundreds of records then it is best option
  3. NSKeyArchiver/NSCoding - If you want to save objects to local file system you can use this option please refer here for more info on this

Happy coding

Use writeToURL:atomically to save an array with NSNumber objects in it. Just create a URL for the location you want to save your array, and call the method on your array,

NSURL *docURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
saveURL = [docURL URLByAppendingPathComponent:@"MyIndexArray"];  
BOOL success = [myArray writeToURL:saveURL atomically:YES];
NSLog(@"success is: %d",success);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top