0

I have a method in which I save a userdefault in viewWillAppear like so:

self.getUserName = [defineXMLData objectForKey:@"fromUserName"];
                    NSLog(@"Saved Array %@" ,self.getUserName);

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
                [prefs setObject:self.getUserName forKey:@"voucherFromUserName"];
                [prefs synchronize];

I do get data from the array in my NSLog but when I try load the user default into the array it returns null? I tried with another saved user default from another view controller and it works.

I try to load the nsarray in viewDidload like so:

self.voucherArray = [prefs arrayForKey:@"voucherFromUserName"];

Even if I try to use the original array in which I saved the data it still does not work:

self.voucherArray = self.getUserName;
6
  • 1
    I read bad or: voucherFromUserName is different from fromUserNameVoucher ..?, and should be objectForKey, and not arrayForKey Commented Dec 5, 2013 at 16:57
  • 1
    Have you allocate your array [[NSArray alloc] init]; before you try to load data to it. Commented Dec 5, 2013 at 16:57
  • Print self.getUserName to make sure there is something there + @"fromUserName" should be @"voucherFromUserName" when you load. Commented Dec 5, 2013 at 16:59
  • I had a typo mistake, when I copied and pasted into stackoverflow the keys are of the same name. Commented Dec 5, 2013 at 17:12
  • Where do you check for the NSUserDefaults data? In another VC, or in the same file along the line? Commented Dec 5, 2013 at 17:37

1 Answer 1

1

Your problem is that viewWillAppear is called after viewDidLoad so you are probably trying to get the data from the userDefault before it is saved in it

Sign up to request clarification or add additional context in comments.

7 Comments

Yes. You are right. The nsarray is called before I am saving the data. How can i work around this?
from where did you get 'defineXMLData' ? ... why you are saving it in 'viewWillAppear' ?
I want to run my method before loading my nsarray.
sorry I didn't get that ... what exactly are you trying to do?
You can load the data in the init, since they don't require UI, it will run first. Or use delegate to trigger an action the exact moment the web service has obtained data.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.