Question

In the following code, I try to read data from a plist:

 -(void)readPreferences 
    {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSMutableArray * myAppDefaults  = [defaults mutableArrayValueForKey:@"LastList"];
        myCustomObject * savedObject;
        NSUInteger i;
        for (i = 0; i < [myAppDefaults  count]; i++) 
        {
            NSArray * thisArray = [myAppDefaults  objectAtIndex:i];

            savedObject.value1 = [thisArray objectAtIndex:0];
            savedObject.value2 = [thisArray objectAtIndex:1];
            savedObject.value3 = [thisArray objectAtIndex:2];
            savedObject.value4 = [myAppDefaults  objectAtIndex:3];

            [objectsArray addObject:savedObject];
        }

    }

Somehow, when I try to set "savedObject.value1", I get an "EXC_BAD_ACCESS" error.

I realize this is quite likely basic memory management, or pointer/object kind of confusion, but I'm still learning. I hope someone can help me out here. Best regards Sjakelien

Was it helpful?

Solution

You haven't initialized savedObject at the time you set the value1 property. You will need to add:

savedObject = [[myCustomObject alloc] init];

before your for loop in order for it to be a valid object that you can set properties on.

OTHER TIPS

I don't see mutableArrayValueForKey in NSUserDefaults. Try arrayForKey: instead, that will give you NSArray*.

Seems that values returned from defaults are immutable (make sense as you're getting stuff from file).

Check the returned value from mutableArrayValueForKey, I suspect it's nil.

On second look your code does not make much sense. At no point you allocate savedObjects but you repeatedly insert it into array. Even the way you are extracting the values does not seem correct.

I would suggest looking at NSUserDefautls reference in Apple documentation, it has examples of basic usage.

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