1

I have read many question how to save custom objects but I m not getting a idea. I don't know what to do next thats why I am asking a question.

Save custom object in plist file

NSLog(@"%@",self.drawingView.pathArray);
NSData *arrayData = [NSKeyedArchiver archivedDataWithRootObject:self.drawingView.pathArray];
[arrayData writeToFile:[DOCUMENTPATH stringByAppendingPathComponent:@"1.plist"] atomically:YES];

Console o/p

(
    "<PenTool: 0xa031f80;>",
    "<PenTool: 0x8b2b360;>",
    "<PenTool: 0xa03aca0;>",
    "<PenTool: 0x8b38780;>"
)

The above code works fine. Save in plist. Now I want to get back all the objects from that plist.

Read from plist

NSData *data=[NSData dataWithContentsOfFile:[DOCUMENTPATH stringByAppendingPathComponent:@"1.plist"]];
self.pathArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];

Here I am getting a data. but not a array. I have read about that but there are saying you have to unarhive data. But i don't have idea how to unarhive.

EDIT

I know I have to use below two method but not getting a idea.

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder {
}

Is there any other way to store custom objects in file.

modusCell
  • 13,151
  • 9
  • 53
  • 80
user3772344
  • 153
  • 2
  • 10

2 Answers2

2

Your PenTool class needs to implement NSCoding. This is described the Archives and Serialisations Programming Guide

This question has a couple of useful examples.

These code snippets are from the Archives and Serialisations Programming Guide, the keys are strings.

- (void)encodeWithCoder:(NSCoder *)coder {
   [coder encodeObject:self.firstName forKey:ASCPersonFirstName];
   [coder encodeObject:self.lastName forKey:ASCPersonLastName];
   [coder encodeFloat:self.height forKey:ASCPersonHeight];
}


- (id)initWithCoder:(NSCoder *)coder {
   self = [super init];
   if (self) {
      _firstName = [coder decodeObjectForKey:ASCPersonFirstName];
      _lastName = [coder decodeObjectForKey:ASCPersonLastName];
      _height = [coder decodeFloatForKey:ASCPersonHeight];
   }
   return self;
}
Community
  • 1
  • 1
Lev Landau
  • 788
  • 3
  • 16
0

Basically plist is a way of saving data in NSDictionary, so I guess you should leave the array alone and start using dictionary in that case

Yossi
  • 2,525
  • 2
  • 21
  • 24