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() メソッドを適切に実装していることを確認しました。
ヒントはありますか?私はこれで本当に迷っています。
ありがとう!
-マット