Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

erdinc27

macrumors regular
Original poster
Jul 20, 2011
168
1
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
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:

erdinc27

macrumors regular
Original poster
Jul 20, 2011
168
1
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
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 ?

after spending time in Apple's developer page i found the solution. i change this line
Code:
NSMutableArray *arrNew = [NSKeyedUnarchiver unarchiveObjectWithData:tmp];
to this
Code:
NSKeyedUnarchiver *decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:tmp];
    
NSArray *arrNew = [decoder decodeObjectForKey:@"logo"];
now it works fine.
 

MattInOz

macrumors 68030
Jan 19, 2006
2,760
0
Sydney
If you found the answer you should make as resolved.

One question are you archiving images in to your plist as binary data or are they URL's to the image files?

Even if they are URL's is the only difference in the file name going to be the file type suffix?
 

erdinc27

macrumors regular
Original poster
Jul 20, 2011
168
1
If you found the answer you should make as resolved.

One question are you archiving images in to your plist as binary data or are they URL's to the image files?

Even if they are URL's is the only difference in the file name going to be the file type suffix?

i keep just the URL of the image files. Their addresses also is different not only suffix.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.