Save array of objects with properties to plist
Asked Answered
D

3

6

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?

Dollop answered 10/12, 2012 at 12:45 Comment(1)
Have you implemented NSCoding in your Bug class??Barranca
M
1

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 and initWithCoder: 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.

- (void)encodeWithCoder:(NSCoder *)coder {
    [coder encodeObject:_title forKey:@"title"];
    [coder encodeFloat:_rating forKey:@"rating"];
    NSData *image = UIImagePNGRepresentation(_thumbImage);
    [coder encodeObject:(image) forKey:@"thumbImage"];
}


- (id)initWithCoder:(NSCoder *)coder {
    _title = [coder decodeObjectForKey:@"title"];
    _rating = [coder decodeFloatForKey:@"rating"];
    NSData *image = [coder decodeObjectForKey:@"thumbImage"];
    _thumbImage = [UIImage imageWithData:image];
    return self;
}

Even this will help you.

Mev answered 10/12, 2012 at 12:57 Comment(0)
B
1

Implement NSCoding in your BugData class as below

- (void)encodeWithCoder:(NSCoder *)coder {
    [coder encodeFloat:title forKey:@"title"];
    [coder encodeFloat:rank forKey:@"rank"];
    [coder encodeObject:UIImagePNGRepresentation(thumbImage) forKey:@"thumbImageData"];
}




- (id)initWithCoder:(NSCoder *)coder {
    title = [coder decodeFloatForKey:@"title"];
    rank = [coder decodeFloatForKey:@"rank"];
    NSData *imgData = [coder decodeObjectForKey:@"thumbImageData"];
    thumbImage = [UIImage imageWithData:imgData ];
    return self;
}
Barranca answered 10/12, 2012 at 13:6 Comment(0)
F
0

BugData must implement the NSCoding protocol.You need this method to encode the data:

- (void) encodeWithCoder: (NSCoder*) encoder;

Where you should provide a NSData object representing the class (decode it with the decoder).
To read the plist you need to implement this method:

-(id) initWithCoder: (NSCoder*) decoder;

Where you read data from decoder and return a BugData object.

Flick answered 10/12, 2012 at 12:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.