1

I'm trying to create an application, and in that I'm receiving some contents from net and loading into and array, if I quit and run the app again i'm losing the data. How can I store the data in the app itself. Should I use some kind of database? If so which one and how can I implement that? I need to store only some string variables.

EDIT: My app is tabbar based app, and have two tabs. Loading the arrays in one tab and loading that array in a table in the other VC. once I load the array and move to the second tab, the table is loaded with the array. If i go back and add few more values and then if i check, the added datas is not displayed in the table. And also, when I quit the app the table lose all the values, I need to keep the values in the table, even after i quit the app. How can I do that? here is my code: in viewdidload:

 NSUserDefaults *simplePlistDb = [NSUserDefaults standardUserDefaults];
        [simplePlistDb setBool:YES forKey:@"isItWorking"];
        [simplePlistDb setObject:alertList forKey:@"myArray"];
        [simplePlistDb synchronize];

in cellforrowatindexpath:-

NSUserDefaults *simplePlistDb = [NSUserDefaults standardUserDefaults];
    BOOL flag = [simplePlistDb boolForKey:@"isItWorking"];
    if (flag)
    {
        NSArray *myArray = [simplePlistDb objectForKey:@"myArray"];
        for (NSString *str in myArray){
            NSLog(@"Str:%@", str);
            [loadArray addObject:str];
        }
        cell.textLabel.text = [loadArray objectAtIndex:indexPath.row];
    }

Thank you.

Mithun
  • 459
  • 1
  • 10
  • 32

3 Answers3

2

You will want to look into Core Data if you want to save a decent amount of data to the iPhone. However, if not, you can just load the items in the array into a plist and load them from there at launch.

Plist Writing: How to create a new custom property list in iPhone Applications

CoreData: http://www.raywenderlich.com/934/core-data-tutorial-getting-started

Edit--->

As Nekto said, you can also use NSUserDefaults, but I advise mainly using NSUserDefaults for simple NSStrings and integers.

Community
  • 1
  • 1
max_
  • 24,076
  • 39
  • 122
  • 211
1

I think for your purposes NSUserDefaults will be enough : NSUserDefaults Class Reference.

Examples:

// save
NSUserDefaults *simplePlistDb = [NSUserDefaults standardUserDefaults];
[simplePlistDb setBool:YES forKey:@"isItWorking"];
[simplePlistDb setObject:[NSArray arrayWithObjects:@"very", @"cool", nil]];
[simplePlistDb synchronize];

// restore
NSUserDefaults *simplePlistDb = [NSUserDefaults standardUserDefaults];
BOOL flag = [simplePlistDb boolForKey:@"isItWorking"];
if (flag)
{
    NSArray *myArray = [simplePlistDb objectForKey:@"myArray"];
    for (NSString *str in myArray)
        NSLog(@"%@", str);
}
Nekto
  • 17,837
  • 1
  • 55
  • 65
  • I need to load only string variables, so should I go for NSUserDefaults? – Mithun Sep 29 '11 at 08:58
  • How much objects do you want to save? – Nekto Sep 29 '11 at 09:00
  • that depends on the user, but mostly around 50. At maximum. – Mithun Sep 29 '11 at 09:01
  • Then you can use `NSUserDefaults` or create your own `plist` file. The easiest way it to use `NSUserDefaults`. – Nekto Sep 29 '11 at 09:07
  • hi, is it possible to restore the stored strings into a tableview? how can I implement that? – Mithun Sep 29 '11 at 10:13
  • i did and it is working, but there is problem. I have edited the main question. Please check it, and help. thanks. – Mithun Sep 29 '11 at 11:20
  • After adding new data you should reload table: `[self.tableView reloadData];`. And of course you should manually save it. – Nekto Sep 29 '11 at 11:24
  • still it is not working. I'm not able to see the new entry. :( here is my code: in ViewDidLoad- NSUserDefaults *simplePlistDb = [NSUserDefaults standardUserDefaults]; [simplePlistDb setBool:YES forKey:@"isItWorking"]; NSArray *arrays = [[NSArray alloc]init]; [simplePlistDb setObject:alertList forKey:@"myArray"]; [simplePlistDb synchronize]; [self.tableView reloadData]; – Mithun Sep 29 '11 at 11:32
  • and in cellForRowAtIndexPath:- NSUserDefaults *simplePlistDb = [NSUserDefaults standardUserDefaults]; BOOL flag = [simplePlistDb boolForKey:@"isItWorking"]; if (flag) { NSArray *myArray = [simplePlistDb objectForKey:@"myArray"]; for (NSString *str in myArray){ NSLog(@"Str:%@", str); [loadArray addObject:str]; } } cell.textLabel.text = [loadArray objectAtIndex:indexPath.row]; any idea?? – Mithun Sep 29 '11 at 11:33
  • Please, open new answer and describe problem there with your code. It's very difficult to view code in comments – Nekto Sep 29 '11 at 12:36
  • Why would you have a for loop in the cellForRowAtIndex Method?! – max_ Sep 29 '11 at 12:36
1

You can use NSUserDefaults in order to store your data till the time your app is installed in your device.

How to write in NSuserDefaults:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"yourObject1" forKey:@"correspopndingKey1"];
[defaults setObject:@"yourObject2" forKey:@"correspopndingKey2"];
[defaults synchronize];

How to read from NSuserDefaults:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *temp1 = [defaults objectForKey:@"correspopndingKey1"];
NSString *temp2 = [defaults objectForKey:@"correspopndingKey2"];

You can store NSArray in a similar way.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:objArray forKey:@"correspopndingKey"];
[defaults synchronize];
Developer
  • 6,375
  • 12
  • 58
  • 92