Time for action – loading and saving the last played game
Follow these steps to load and save the current state:
- In
World.h
, declare methods to serialize and deserialize data:+(NSDictionary *) serialize; +(void) deserialize: (NSDictionary *) dict;
- Implement these serializers with the following lines of code:
+(NSDictionary *) serialize { return @{ @"level": [NSNumber numberWithInt:level], @"gold": [NSNumber numberWithInt:gold], @"damage": [NSNumber numberWithInt:damage], @"hitpoints": [NSNumber numberWithInt:hitpoints] }; } +(void) deserialize: (NSDictionary *) dict { level = [(NSNumber *) dict[@"level"] intValue]; gold = [(NSNumber *) dict[@"gold"] intValue]; damage = [(NSNumber *) dict[@"damage"] intValue]; hitpoints = [(NSNumber *) dict[@"hitpoints"] intValue]; }
- In
MainMenu.m
, addWorld.h
to the import section and update the...