Omer Waqas Khan
Omer Waqas Khan

Reputation: 2413

NSMutableArray and NSUserdefaults issue iPhone

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSUserDefaults *somet = [NSUserDefaults standardUserDefaults];

    NSMutableArray *anArr = [somet objectForKey:@"somedata"] ;

    NSLog(@"anArr::: %@",anArr);

}

-(IBAction)addsomething:(id)sender{

   if (something == nil) {
        array = [[NSMutableArray alloc] init];
    }
    NSLog(@"textfieldvalue::: %@", textfield.text);

    [array addObject:textfield.text];

    NSUserDefaults *something = [NSUserDefaults standardUserDefaults];

    [something setObject:array forKey:@"somedata"];

    array = [something objectForKey:@"somedata"];

    NSLog(@"array:: %@", array);

}

Using the above code, I am trying to save data dynamically in NSMutableArray and then saving it in NSUserDefaults. But on retrieving it I am getting (null). Even data is not being saved in array.

How to save data properly?

EDITED:

2012-03-15 15:17:18.431 check[2686:40b] anArr::: (null)
2012-03-15 15:17:30.160 check[2686:40b] textfieldvalue::: a
2012-03-15 15:17:30.162 check[2686:40b] array::: (null)

Upvotes: 0

Views: 5430

Answers (5)

Tirth
Tirth

Reputation: 7789

Try the following code:

- (void)viewDidLoad
{
 [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

NSUserDefaults *somet = [NSUserDefaults standardUserDefaults];

NSMutableArray *anArr = [[NSMutableArray alloc] init];
 anArr = [somet objectForKey:@"somedata"] ;
[somet synchronize];

NSLog(@"anArr::: %@",anArr);

}

-(IBAction)addsomething:(id)sender{

NSLog(@"textfieldvalue::: %@", textfield.text);
//Here u have to check array is in memory allocate or not 
[array addObject:textfield.text];

NSUserDefaults *something = [NSUserDefaults standardUserDefaults];

[something setObject:array forKey:@"somedata"];
[something synchronize];

array = [something objectForKey:@"somedata"];

NSLog(@"array:: %@", array);

}

Upvotes: 3

Matthias Bauch
Matthias Bauch

Reputation: 90117

no offense, but your code is a big pile of crap.

  1. variables named something are not useful. Period.
  2. you have a variable named something in your local method. And you have a variable named something that is part of your class. The "local variables hides instance variable" warnings are there for a reason!
    Probably that is where all your problems come from. I think you don't really understand the concept of local variables. If that is the case you should read more about basic Objective-C stuff.
  3. You check if something is nil and then you don't change something but you allocate an array. Those two have nothing to do with each other. Don't write such code.

I fixed it for you:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSArray *anArr = [userDefaults objectForKey:@"somedata"];
    NSLog(@"anArr::: %@",anArr);
}

-(IBAction)addsomething:(id)sender{
    NSMutableArray *array = [[NSMutableArray alloc] init];
    NSLog(@"textfieldvalue::: %@", textfield.text);
    [array addObject:textfield.text];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:array forKey:@"somedata"];
    array = [userDefaults objectForKey:@"somedata"];

    // synchronize is only needed while debugging (when you use the stop button in Xcode)
    // you don't need this in production code. remove it for release
    [userDefaults synchronize];

    NSLog(@"array:: %@", array);
}

However, most likely you want to save objects to the array you got from userDefaults. That won't work with local variables!

You would use something like this. But don't copy that verbatim. Try to understand it, and read more about local variables.

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
array = [[userDefaults objectForKey:@"somedata"] mutableCopy];
if (!array) {
    // create array if it doesn't exist in NSUserDefaults
    array = [[NSMutableArray alloc] init];
}
NSLog(@"array in viewDidLoad: %@",array);



NSLog(@"textfieldvalue::: %@", textField.text);
[array addObject: textField.text];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:array forKey:@"somedata"];

// synchronize is only needed while debugging (when you use the stop button in Xcode)
// you don't need this in production code. remove it for release
[userDefaults synchronize];

Upvotes: 3

hchouhan02
hchouhan02

Reputation: 946

try this when you are saving value into NSUserDefaults

- (void)savingObject:(NSString *)keyName data:(id)val
{
    NSData *dataVal = [NSKeyedArchiver archivedDataWithRootObject:val];
    [defaults setObject:dataVal forKey:keyName];
    [defaults synchronize];
}

and then when you want to get that use this

- (id)gettingObject:(NSString *)keyName
{
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:keyName];
    return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}

i think this helps you

Upvotes: 0

Vignesh
Vignesh

Reputation: 10251

add this line ,

[something synchronize]

before retrieving data.

Upvotes: 0

fannheyward
fannheyward

Reputation: 19277

You need to call [[NSUserDefaults standardUserDefaults] synchronize]; after you setObject:forKey.

[something setObject:array forKey:@"somedata"];
[something synchronize];

Upvotes: 2

Related Questions