user2266844
user2266844

Reputation: 11

problems getting NSUserdefaults

Im new at coding in XCode and I tried so much to save information, but it's not working out. I googled, but I still did not find anything.

I take a mutable array called classNames and tried to save information from a text field (classField), but it's not storing with in the array.

I have another problem: I'm using XCode having iOS 5.1, and I upgraded to Xcode having iOS 6.1, but it gives me an error saying "CONNECTION INTERRUPTED" and none of the buttons or functions work, but it works with XCode having iOS 5.1 I need help, thank you.

#import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController
    @synthesize orignalClass;
    @synthesize otherClassName;
    @synthesize editClassLabel;
    @synthesize displayLabel;
    @synthesize doSomethingButton;
    @synthesize classField;
    @synthesize testField;
    @synthesize classFieldFireTheme;
    @synthesize testFieldFireTheme;

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        _classNames = [[NSMutableArray alloc] init];
        _classNames = [defaults objectForKey:@"classNames"];
        //NSLog(@" %@", [defaults objectForKey:@"classNames"]);


    }


- (void)viewDidUnload
{
    [self setDoSomethingButton:nil];
    [self setClassField:nil];
    [self setTestField:nil];
    [self setClassFieldFireTheme:nil];
    [self setTestFieldFireTheme:nil];
    [self setDisplayLabel:nil];
    [self setOrignalClass:nil];
    [self setEditClassLabel:nil];
    [self setOtherClassName:nil];
    [self setTestClass:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (IBAction)classtextFieldDoneEdtiting:(id)sender {
    [sender resignFirstResponder];
}
- (IBAction)testClassTextFieldDoneEdtiting:(id)sender {
    [sender resignFirstResponder];
}
- (IBAction)TestTextFieldDoneEdtiting:(id)sender {
    [sender resignFirstResponder];
}
- (IBAction)ChangeClassTextFieldDoneEdtiting:(id)sender {
    [sender resignFirstResponder];
}
- (IBAction)ChangeClassToTextFieldDoneEdtiting:(id)sender {
    [sender resignFirstResponder];
}

- (IBAction)addClass:(id)sender {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    _classNames = [defaults objectForKey:@"classNames"];
    _classSize = [_classNames count];
    NSLog(@" %d", [_classNames count]);

    if(_classSize <=9){

        [_classNames insertObject: classField.text atIndex: [_classNames count]];
        NSLog(@"ClassNames#2::: %@", _classNames);
        NSLog(@"textfieldvalue::: %@", classField.text);
        [defaults setObject:_classNames forKey:@"classNames"];
        [[NSUserDefaults standardUserDefaults] synchronize];

        NSLog(@" %d", [_classNames count]);
        NSLog(@"ClassNames#3::: %@", _classNames);

   }else{
        displayLabel.text = @"You exceeded the limit of classes being stores!!!!";
   }

}

Upvotes: 1

Views: 347

Answers (2)

matt
matt

Reputation: 535159

The problem is that when an array comes out of NSUserDefaults it is not a mutable array. Even if what you put in was an NSMutableArray, what comes out is an ordinary NSArray. You need to use mutableCopy on that array to make it mutable. Now you can store into it, and then put it back into NSUserDefaults.

Upvotes: 4

bdesham
bdesham

Reputation: 16089

This code can't work. You're declaring defaults within the addClass: method, and then trying to access it from the viewDidLoad method, which you can't do. Add a line to the top of viewDidLoad like

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

If you need more help, we need more information about the error you're getting.

Upvotes: 1

Related Questions