Trouble decoding with NSKeyedUnarchiver

℡╲_俬逩灬. 提交于 2019-12-11 03:42:37

问题


I am writing an app targeted at iOS 4.0 on XCode 3.2.3 where I store some data when the app closes using NSCoder protocol. Saving seems to work fine, the problem is retrieving the data from the saved files. My save method looks like this:

-(void)saveMusicalWorksToMemory{
    // create the save paths
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory =  [paths objectAtIndex:0];
    NSString *mwPath = [documentsDirectory stringByAppendingPathComponent:kMusicalWorksLocalFileSuffix];
    NSString *fwPath = [documentsDirectory stringByAppendingPathComponent:kFeaturedWorksLocalFileSuffix];


    NSMutableData *musicalWorksData;
    NSMutableData *featuredWorksData;
    NSKeyedArchiver *mwEncoder;
    NSKeyedArchiver *fwEncoder;

    musicalWorksData = [NSMutableData data];
    featuredWorksData = [NSMutableData data];

    mwEncoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:musicalWorksData];
    fwEncoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:featuredWorksData];

    // encode the objects
    [mwEncoder encodeObject:musicalWorks forKey:kMusicalWorksArchiveKey];
    [fwEncoder encodeObject:featuredWorks forKey:kFeaturedWorksArchiveKey];
    [mwEncoder finishEncoding];
    [fwEncoder finishEncoding];

    // write to files
    [musicalWorksData writeToFile:mwPath atomically:YES];
    [featuredWorksData writeToFile:fwPath atomically:YES];

    [mwEncoder release];
    [fwEncoder release];
}

And here is the method where I read from the files:

-(void)loadMusicalWorksFromMemory{
    // get the path to the locally saved data
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory =  [paths objectAtIndex:0];
    NSString *mwPath = [documentsDirectory stringByAppendingPathComponent:kMusicalWorksLocalFileSuffix];
    NSString *fwPath = [documentsDirectory stringByAppendingPathComponent:kFeaturedWorksLocalFileSuffix];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:mwPath] && [fileManager fileExistsAtPath:fwPath]){
        NSMutableData *mwData;
        NSMutableData *fwData;
        NSKeyedUnarchiver *mwDecoder;
        NSKeyedUnarchiver *fwDecoder;
        NSMutableArray *tempMusicalWorks;
        NSMutableArray *tempFeaturedWorks;

        // unarchive the file data
        mwData = [NSData dataWithContentsOfFile:mwPath];
        fwData = [NSData dataWithContentsOfFile:fwPath];
        mwDecoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:mwData];
        fwDecoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:fwData];

        tempMusicalWorks = [mwDecoder decodeObjectForKey:kMusicalWorksArchiveKey];
        tempFeaturedWorks = [fwDecoder decodeObjectForKey:kFeaturedWorksArchiveKey];

        [mwDecoder finishDecoding];
        [fwDecoder finishDecoding];
        [mwDecoder release];
        [fwDecoder release];

        // restore the content
        [self setMusicalWorks:tempMusicalWorks];
        [self setFeaturedWorks:tempFeaturedWorks];


    }else{
        NSLog(@"MusicalWorkManager::loadMusicalWorksFromMemory - No file found at given path");
    }
}

The weird thing is that when reading the data from memory and debugging, I see that mwData and fwData are in fact not nil, and have roughly the right size proportions to each other (about 2:1). The problem is though that the decoded arrays (tempMusicalWorks and tempFeaturedWorks) have zero entries (not nil though). This leads me to believe that the problem is actually in the decoding.

For the record, I have also checked that items are being saved in the saveMusicalWorksToMemory() method. The arrays I am passing in through this method are indeed populated with stuff. Also, I have checked that I am implementing the initWithCoder() and encodeWithCoder() methods properly for the objects being encoded and decoded.

Any hints? I'm really lost on this one.

Thanks!

-Matt

来源:https://stackoverflow.com/questions/3358074/trouble-decoding-with-nskeyedunarchiver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!