0

I've seen a few posts about how you can't save an object based NSMutableArray in NSUserDefaults, but that there is a way of doing it using [NSKeyedArchiver] and encoding it with NSCoder although I'm struggling to understand how to do it and was hoping someone could help.

Student *student1 = [Student studentWithImage:[UIImage imageNamed:@"default"] withForename:@"John" withSurname:@"Smith" withAddress:@"3 Fake Road, Faketown, FA31 KEE" withDateOfBirth:[dateFormatter dateFromString:@"17-Jul-93"] withAge:24];
[studentArray addObject:student1];

I want this and a few other similar Students to be saved to my NSMutableArray studentArray. I want to then save this array to NSUserDefaults and load it back up again.

I have seen this post here which looks like is the correct answer to my question, but I need help implementing it to my code as I'm having difficulty understanding it! Thanks

8
  • you want save studentArray in userdafaults ? Commented Aug 2, 2017 at 9:02
  • @KKRocks thats correct, I need the array to be saved containing its properties like student Image, name, date of birth ect. Commented Aug 2, 2017 at 9:07
  • @Mahesh check my answer and add your property as i mentioned. Commented Aug 2, 2017 at 9:08
  • What didn't you understand about the linked question? What part is unclear? Because currently, the answers from KKRocks is just a simple adaption to your case, but without real more new informations or explanation. It's clearly a copy/paste from the other answer with a "Replace" (more or less). Commented Aug 2, 2017 at 9:21
  • @Larme The answer in the previous question was very clear, I just stupidly misunderstood where the -(void)encodeWithCoder and initWithCoder were placed. The first line in the answer below answered my question immediately. Commented Aug 2, 2017 at 9:27

1 Answer 1

3

First add this in Student class

- (void)encodeWithCoder:(NSCoder *)coder;
{
    [coder encodeObject:self.Forename forKey:@"keyForename"];
    // do same for all property
}

- (id)initWithCoder:(NSCoder *)coder;
{
    self = [[Student alloc] init];
    if (self != nil)
    {
        self.Forename = [coder decodeObjectForKey:@"keyForename"];
        // do same other property here
    }   
    return self;
}

Store Array

[studentArray addObject:student1];
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:studentArray] forKey:@"mySavedArray"];

Retrieve Array

NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *savedArray = [currentDefaults objectForKey:@"mySavedArray"];
if (savedArray != nil)
{
        NSArray *oldArray = [NSKeyedUnarchiver unarchiveObjectWithData:savedArray];
        if (oldArray != nil) {
                customObjectArray = [[NSMutableArray alloc] initWithArray:oldArray];
        } else {
                customObjectArray = [[NSMutableArray alloc] init];
        }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This has worked excellently, although the only issue I'm having now is that the studentImage isn't saving. I have used [coder encodeObject:self.studentImage forKey:@"keyStudentImage"]; but when loading the array, images don't load?
I've worked it out, don't worry: [coder encodeObject:UIImagePNGRepresentation(self.studentImage) forKey:@"keyStudentImage"]; in the encodeWithCoder and self.studentImage = [UIImage imageWithData:[coder decodeObjectForKey:@"keyStudentImage"]]; in the initWithCoder. Thanks Again KKRocks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.