I have a Mutable Array containing different objects such as strings, UIImage , etc.
They are sorted like this:
Example:
BugData *bug1 = [[BugData alloc]initWithTitle:@"Spider" rank:@"123" thumbImage:[UIImage imageNamed:@"1.jpeg"]];
...
...
NSMutableArray *bugs = [NSMutableArray arrayWithObjects:bug1,bug2,bug3,bug4, nil];
So basically it’s an array with objects with different properties.
I Tried to save a single string to a file with the next code and it’s working fine but when I try to save the array with the objects, i get an empty plist file.
NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString * path = [docsDir stringByAppendingPathComponent:@"data.plist"];
NSLog(@"%@",bugs); //Making sure the array is full
[bugs writeToFile:path atomically:YES];
What am I doing wrong?
When you write a string or any primitive data to plist it can be saved directly. But when you try to save an object, you need to use NSCoding.
You have to implement two methods
encodeWithCoder:to write andinitWithCoder:to read it back in your BugData Class.EDIT:
Something like this :
Change Float to Integer or String or Array as per your requirement and give a suitable key to them.
Even this will help you.