2

我正在 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 实际上不是 nil,并且彼此之间的大小比例大致正确(大约 2:1)。问题在于解码后的数组(tempMusicalWorks 和 tempFeaturedWorks)的条目为零(虽然不是 nil)。这使我相信问题实际上出在解码中。

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

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

谢谢!

-马特

4

0 回答 0