Hi,
I need to save my own created class to file, I found on the internet, that good approach is to use NSKeyedArchiver and NSKeyedUnarchiver My class definition looks like this:
@interface Game : NSObject <NSCoding> {
NSMutableString *strCompleteWord;
NSMutableString *strWordToGuess;
NSMutableArray *arGuessedLetters; //This array stores characters
NSMutableArray *arGuessedLettersPos; //This array stores CGRects
NSInteger iScore;
NSInteger iLives;
NSInteger iRocksFallen;
BOOL bGameCompleted;
BOOL bGameOver;
}
I've implemented methods initWithCoder:
and encodeWithCoder:
this way:
- (id)initWithCoder:(NSCoder *)coder
{
if([coder allowsKeyedCoding])
{
strCompleteWord = [[coder decodeObjectForKey:@"CompletedWord"] copy];
strWordToGuess = [[coder decodeObjectForKey:@"WordToGuess"] copy];
arGuessedLetters = [[coder decodeObjectForKey:@"GuessedLetters"] retain];
// arGuessedLettersPos = [[coder decodeObjectForKey:@"GuessedLettersPos"] retain];
iScore = [coder decodeIntegerForKey:@"Score"];
iLives = [coder decodeIntegerForKey:@"Lives"];
iRocksFallen = [coder decodeIntegerForKey:@"RocksFallen"];
bGameCompleted = [coder decodeBoolForKey:@"GameCompleted"];
bGameOver = [coder decodeBoolForKey:@"GameOver"];
}
else
{
strCompleteWord = [[coder decodeObject] retain];
strWordToGuess = [[coder decodeObject] retain];
arGuessedLetters = [[coder decodeObject] retain];
// arGuessedLettersPos = [[coder decodeObject] retain];
[coder decodeValueOfObjCType:@encode(NSInteger) at:&iScore];
[coder decodeValueOfObjCType:@encode(NSInteger) at:&iLives];
[coder decodeValueOfObjCType:@encode(NSInteger) at:&iRocksFallen];
[coder decodeValueOfObjCType:@encode(BOOL) at:&bGameCompleted];
[coder decodeValueOfObjCType:@encode(BOOL) at:&bGameOver];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder
{
if([coder allowsKeyedCoding])
{
[coder encodeObject:strCompleteWord forKey:@"CompleteWord"];
[coder encodeObject:strWordToGuess forKey:@"WordToGuess"];
[coder encodeObject:arGuessedLetters forKey:@"GuessedLetters"];
//[coder encodeObject:arGuessedLettersPos forKey:@"GuessedLettersPos"];
[coder encodeInteger:iScore forKey:@"Score"];
[coder encodeInteger:iLives forKey:@"Lives"];
[coder encodeInteger:iRocksFallen forKey:@"RocksFallen"];
[coder encodeBool:bGameCompleted forKey:@"GameCompleted"];
[coder encodeBool:bGameOver forKey:@"GameOver"];
}
else
{
[coder encodeObject:strCompleteWord];
[coder encodeObject:strWordToGuess];
[coder encodeObject:arGuessedLetters];
//[coder encodeObject:arGuessedLettersPos];
[coder encodeValueOfObjCType:@encode(NSInteger) at:&iScore];
[coder encodeValueOfObjCType:@encode(NSInteger) at:&iLives];
[coder encodeValueOfObjCType:@encode(NSInteger) at:&iRocksFallen];
[coder encodeValueOfObjCType:@encode(BOOL) at:&bGameCompleted];
[coder encodeValueOfObjCType:@encode(BOOL) at:&bGameOver];
}
}
And I use these methods to archive and unarchive data:
[NSKeyedArchiver archiveRootObject:currentGame toFile:strPath];
Game *currentGame = [NSKeyedUnarchiver unarchiveObjectWithFile:strPath];
I have two problems.
1) As you can see, lines with arGuessedLettersPos
is commented, it's because every time I try to encode this array, error comes up(this archiver cannot encode structs), and this array is used for storing CGRect structs.
I've seen solution on the internet. The thing is, that every CGRect
in the array is converted to an NSString
(using NSStringFromCGRect()
) and then saved. Is it a good approach?
2)This is bigger problem for me. Even if I comment this line and then run the code successfully, then save(archive) the data and then try to load (unarchive) them, no data is loaded. There aren't any error but currentGame
object does not have data that should be loaded.
Could you please give me some advice? This is first time I'm using archivers and unarchivers.
Thanks a lot for every reply.