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.

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

yes that i have to do NSCoding.. but your question link not providing any answer for NSCoding. i dont have any idea about NSCoding. please guide me
See the Encoding and Decoding chapter in the Archives and Serialisations Programming Guide.
Hopefully the above copy & paste from programming guide explains what you need to do.
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

2 Comments

But I have custom objects with all the drawing porperty.. how can I take dictionary here
Is there any other way to store custom objects

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.