user3162102
user3162102

Reputation: 105

How to store information persistently in iOS?

I have an application which loads a list of countries in a tableview. First time when the app is downloaded it should display the entire list, when the user selects a country this should be added to an array. I'm not getting how to track how many number of countries user has selected for the first time and from next time whenever the user open the app I should display only the selected countries. In what way can I implement this? When I searched in net got to know static but unable to implement it.

I'm new to iPhone development. Any help would be appreciated.

Upvotes: 0

Views: 127

Answers (3)

footyapps27
footyapps27

Reputation: 4042

What you are looking for is persistent storage.

The various ways of persistent storage in iOS are:-

  1. Property Lists
  2. SQLite
  3. Core Data
  4. User Defaults (NSUserDefaults).

Depending on the kind & size of data you want to store, one of the following could be used.

If your requirement says that you only have to persist a NSArray, then it is a good idea to go for Property Lists or NSUser Defaults.

For reading & writing Property List you can find the documentation in Apple Docs

For NSUser Defaults usage, you can find a good tutorial here

Upvotes: 0

tanz
tanz

Reputation: 2547

You have different options to store information persistently: Core Data, SQLite, etc. A simple one to use in this case is NSUserDefault. If you keep the list of selected countries in an NSArray you can store and retrieve this array as follows:

//To set your array
[[NSUserDefaults standardUserDefaults]setObject:(NSArray *)yourArray forKey:@"userSelection"];
[[NSUserDefaults standardUserDefaults]synchronize];

//To retrieve your array
NSArray *myArray = [[NSUserDefaults standardUserDefaults]objectForKey:@"userSelection"];

To use this option your NSArray however must be a property list - here's the Apple documentation to learn what a property list is. If you want to store other type of objects you should look into different solution (.e.g Core Data).

Upvotes: 1

Volker
Volker

Reputation: 4660

You need to store the selection persistently. Nothing to do with a static variable, which would get nulled when quitting the app. Look for example at NSUserPreferences as one way to store user preferences.

Upvotes: 0

Related Questions