Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Saving and retrieving data from NSUserDefaults

Hello all, I am trying to save three different types of objects, one of which is an NSString and two custom classes. The method that is posted below used to work in IOS 7 but no longer works and I am trying to figure out why. Also, the calls to self.___array are 3 different global NSMutableArrays. What is wrong with the current implementation and what do I do to fix it? Thanks in advance guys1

-(void)saveUserData { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if ([self.classHierarchyDelegateMutableArray count]>0)
{

    for (NSInteger i = 0; i<[self.homeworkItemsArray count]; i++)
    {
        homeworkObject* homeworkItem = [self.homeworkItemsArray objectAtIndex:i];
        NSData* homeworkData = [NSKeyedArchiver archivedDataWithRootObject:homeworkItem];
        [self.homeworkItemsArray replaceObjectAtIndex:i withObject:homeworkData];
    }

    for (NSInteger i = 0; i<[self.upcomingEventItems count]; i++)
    {
        upcomingEventObject* upcomingEvent = [self.upcomingEventItems objectAtIndex:i];
        NSData* upcomingEventData = [NSKeyedArchiver archivedDataWithRootObject:upcomingEvent];
        [self.upcomingEventItems replaceObjectAtIndex:i withObject:upcomingEventData];
    }
    NSData* homeworkItemsArray = [NSKeyedArchiver archivedDataWithRootObject:self.homeworkItemsArray];
    NSData* upcomingEventsArray = [NSKeyedArchiver archivedDataWithRootObject:self.upcomingEventItems];

    [defaults setObject:self.classHierarchyDelegateMutableArray forKey:@"classHierarchyArray"];
    [defaults setObject:homeworkItemsArray forKey:@"homeworkItemsArray"];
    [defaults setObject:upcomingEventsArray forKey:@"upcomingEventItemsArray"];
}
else
{
    [defaults setObject:nil forKey:@"classHierarchyArray"];
    [defaults setObject:nil forKey:@"homeworkItemsArray"];
    [defaults setObject:nil forKey:@"upcomingEventItemsArray"];
}

[defaults synchronize];

}

-(void)loadUserData { if ([[NSUserDefaults standardUserDefaults]objectForKey:@"classHierarchyArray"]) { self.classHierarchyDelegateMutableArray = [[NSUserDefaults standardUserDefaults]objectForKey:@"classHierarchyArray"];

    NSMutableArray* homeworkArray = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults]objectForKey:@"homeworkItemsArray"]];

    for (NSInteger i = 0; i<[homeworkArray count]; i++)
    {
        NSData* homeworkData = [homeworkArray objectAtIndex:i];

        homeworkObject* homeworkItem = [NSKeyedUnarchiver unarchiveObjectWithData:homeworkData];

        [self.homeworkItemsArray addObject:homeworkItem];
    }

    NSMutableArray* upcomingEventsArray = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults]objectForKey:@"upcomingEventItemsArray"]];

    for (NSInteger i = 0; i<[upcomingEventsArray count]; i++)
    {
        NSData* upcomingEventsData = [upcomingEventsArray objectAtIndex:i];

        upcomingEventObject* upcomingEvent = [NSKeyedUnarchiver unarchiveObjectWithData:upcomingEventsData];

        [self.upcomingEventItems addObject:upcomingEvent];
    }

}

}