Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>no offense, but your code is a big pile of crap. </p> <ol> <li>variables named <code>something</code> are not useful. Period. </li> <li>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!<br> 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. </li> <li>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. </li> </ol> <p>I fixed it for you:</p> <pre><code>- (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); } </code></pre> <hr> <p>However, most likely you want to save objects to the array you got from userDefaults. That won't work with local variables! </p> <p>You would use something like this. But don't copy that verbatim. Try to understand it, and read more about local variables.</p> <pre><code>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]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload