Search code examples
iphoneobjective-ciosxcode

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)

Solution

  • 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];