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

jscs
  • 63,694
  • 13
  • 151
  • 195
Manesh
  • 528
  • 6
  • 20
  • you want save studentArray in userdafaults ? – KKRocks Aug 02 '17 at 09:02
  • @KKRocks thats correct, I need the array to be saved containing its properties like student Image, name, date of birth ect. – Manesh Aug 02 '17 at 09:07
  • @Mahesh check my answer and add your property as i mentioned. – KKRocks Aug 02 '17 at 09: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). – Larme Aug 02 '17 at 09: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. – Manesh Aug 02 '17 at 09:27
  • If you had said that, we could have just pointed out that part of the answer: "to make your custom object comply to the NSCoder protocol", and oriented our answer/explains what it means to "comply to a protocol" (which is a basic and import knowledge). – Larme Aug 02 '17 at 09:29
  • It wasn't until I saw the answer below that this became clear to me. Thanks for the help anyway @KKRocks its much appreciated. – Manesh Aug 02 '17 at 09:31
  • @mahesh mark as accept if answer helping you. – KKRocks Aug 02 '17 at 09:39

1 Answers1

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];
        }
}
KKRocks
  • 8,222
  • 1
  • 18
  • 84
  • 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? – Manesh Aug 02 '17 at 09:22
  • 1
    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 – Manesh Aug 02 '17 at 09:39