Resolved encode/decode custom object

Discussion in 'iOS Programming' started by erdinc27, Jan 20, 2014.

  1. erdinc27, Jan 20, 2014
    Last edited: Jan 20, 2014

    erdinc27 macrumors regular

    Jul 20, 2011
    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
    - (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.
        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 ?
  2. erdinc27 thread starter macrumors regular

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


    Jan 19, 2006
    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?
  4. erdinc27 thread starter macrumors regular

    Jul 20, 2011
    i keep just the URL of the image files. Their addresses also is different not only suffix.

Share This Page