i have a custom object(TheLogo). i want to save an array that includes this objects to plist. so i encode it and write to plist. but when i cant decode it back.
here how my object looks
TheLogo.m
and here how i written it to plist.
but arrNew count is always 0. how i can decode that objects from plist ?
here how my object looks
TheLogo.m
Code:
- (void) encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.logoID forKey:@"logoID"];
[aCoder encodeObject:self.logoTitle forKey:@"logoTitle"];
[aCoder encodeObject:self.logoThumb forKey:@"logoThumb"];
[aCoder encodeObject:self.logoResim forKey:@"logoResim"];
[aCoder encodeObject:self.logoIllustrator forKey:@"logoIllustrator"];
[aCoder encodeObject:self.logoEPS forKey:@"logoEPS"];
[aCoder encodeObject:self.logoJPG forKey:@"logoJPG"];
}
- (id)initWithCoder:(NSCoder *)decoder
{
if (self = [super init])
{
self.logoID = [decoder decodeObjectForKey:@"logoID"];
self.logoTitle = [decoder decodeObjectForKey:@"logoTitle"];
self.logoThumb = [decoder decodeObjectForKey:@"logoThumb"];
self.logoResim = [decoder decodeObjectForKey:@"logoResim"];
self.logoIllustrator = [decoder decodeObjectForKey:@"logoIllustrator"];
self.logoEPS = [decoder decodeObjectForKey:@"logoEPS"];
self.logoJPG = [decoder decodeObjectForKey:@"logoJPG"];
}
return self;
}
and here how i written it to plist.
Code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *destPath = [documentsDirectory stringByAppendingPathComponent:@"Favorites.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ( ![fileManager fileExistsAtPath:destPath] )
{
NSString *pathToSettingsInBundle = [[NSBundle mainBundle] pathForResource:@"Favorites" ofType:@"plist"];
[fileManager copyItemAtPath:pathToSettingsInBundle toPath:destPath error:nil];
}
NSMutableArray *arrLogo = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 0; i< 10; i++)
{
[arrLogo addObject:[arrParamLogos objectAtIndex:i]];
}
NSMutableData *tmp = [NSMutableData data];
NSKeyedArchiver *encode = [[NSKeyedArchiver alloc] initForWritingWithMutableData:tmp];
[encode encodeObject:arrLogo forKey:@"logo"];
[encode finishEncoding];
[tmp writeToFile:destPath atomically:YES];
// here i try to read it back from plist
NSMutableArray *arrNew = [NSKeyedUnarchiver unarchiveObjectWithData:tmp];
NSLog(@"New Array Count %d", [arrNew count]);
but arrNew count is always 0. how i can decode that objects from plist ?
Last edited: