使用 NSKeyedUnarchiver 解码时出现问题

发布于 2024-09-12 13:47:15 字数 3511 浏览 4 评论 0原文

我正在 XCode 3.2.3 上编写一个针对 iOS 4.0 的应用程序,当应用程序关闭时,我使用 NSCoder 协议存储一些数据。保存似乎工作正常,问题是从保存的文件中检索数据。我的保存方法如下所示:

-(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];
}

这是我从文件中读取数据的方法:

-(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");
    }
}

奇怪的是,当从内存中读取数据并进行调试时,我发现 mwData 和 fwData 实际上不为零,并且大致正确尺寸比例(大约2:1)。问题是解码的数组(tempMusicalWorks 和 tempFeaturedWorks)的条目为零(但不是零)。这让我相信问题实际上出在解码上。

作为记录,我还检查了项目是否保存在 saveMusicalWorksToMemory() 方法中。我通过此方法传入的数组确实填充了内容。另外,我还检查了我是否为正在编码和解码的对象正确实现了 initWithCoder() 和encodeWithCoder() 方法。

有什么提示吗?我真的对这个迷失了。

谢谢!

-马特

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文