Question

Im storing a NsMutableArray in Nsuserdefaults, I think this can be done and its correct, so Im adding a text from a TexField to the Array but when I try to read it in a NSLOG sends null, heres the code:

@property(nonatomic, strong)NSMutableArray *tasks;

NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
    [standardUserDefaults setObject:self.tasks forKey:@"tasks"];
    [self.tasks addObject:textField.text];

    NSLog(@"tasks:%@", [[NSUserDefaults standardUserDefaults]objectForKey:@"tasks"]);

Thanks!!

Was it helpful?

Solution

You need to add the text to the array BEFORE you save it to defaults

NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];

// make sure self.tasks is initialized
if (!self.tasks) self.tasks = [NSMutableArray new];
// add object first
[self.tasks addObject:textField.text];
// now save array
[standardUserDefaults setObject:self.tasks forKey:@"tasks"];

NSLog(@"tasks:%@", [[NSUserDefaults standardUserDefaults]objectForKey:@"tasks"]);

If this is returning null, then

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top