Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8477873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:35:16+00:00 2026-06-10T18:35:16+00:00

I am making an iOS game and need to save the highest level the

  • 0

I am making an iOS game and need to save the highest level the player has reached. I can successfully change a data element in a plist, but for some reason, that data keeps reverting to its original value every time the game restarts. Here is the basic flow of my code:

In the game’s init, get the highest level the player has reached (the original value is 1)

pData = [[PlayerData alloc] init];
currentLevel = [[pData.data valueForKey:@"Highest Level"] intValue];
[self startNewLevel:currentLevel];

‘data’ is an NSMutableDictionary that gets initialized like this in PlayerData:

self.data = [NSMutableDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"playerdata" ofType:@"plist"]];

later, if the player beats the highest level, I increment the ‘highest level’ value and write to the file by calling this function in PlayerData:

-(void) newHighestLevel:(NSString*)path :(int)level{
     [self.data setValue:[NSNumber numberWithInt:level] forKey:path];
     [self.data writeToFile:@"playerdata.plist" atomically:YES]

I know all this is working because I have a level menu that the player can access while playing the game. Each time the player presses the level menu button, a subclass of a UITableView gets created that displays level 1 through the highest level reached. The initialization looks like this:

levelMenu = [[LevelMenu alloc] init:[[pData.data valueForKey:@"Highest Level"] intValue]];

The level menu displays the correct number of levels while in the game (e.g. if the player hasn’t beat any levels and goes to the level menu, it just displays “level 1”; but if the player beats level 1 and goes to the level menu, it displays “level 1” and “level 2”). However, whenever the app gets terminated or the user quits to the games main menu, the value for ‘highest level’ reverts back to 1 and this code:

currentLevel = [[pData.data valueForKey:@"Highest Level"] intValue];

always sets currentLevel to 1 when the player pushes start, no matter how many levels the user beat while playing.

Why is the value changing back? Am I missing something that would make my plist edits permanent?

EDIT:

here is my new ‘newHighestLevel’ method:

-(void) newHighestLevel:(NSString*)path :(int)level{
    [self.data setValue:[NSNumber numberWithInt:level] forKey:path];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    NSString *docfilePath = [basePath stringByAppendingPathComponent:@"playerdata.plist"];
    [self.data writeToFile:docfilePath atomically:YES];
    self.data = [NSMutableDictionary dictionaryWithContentsOfFile:docfilePath];
    BOOL write = [self.data writeToFile:docfilePath atomically:YES];
}

write gets set to YES. If I change ‘docfilePath’ to @”playerdata.plist”, it gets set to NO. Nothing seems to change with the game in either case.

SOLUTION:

-(void) newHighestLevel:(NSString*)path :(int)level{
      [self.data setValue:[NSNumber numberWithInt:level] forKey:path];
      NSString *basePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
      NSString *docfilePath = [basePath stringByAppendingPathComponent:@"playerdata.plist"];
      [self.data writeToFile:docfilePath atomically:YES];
}

and in init

-(id) init{

     NSString *basePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
     NSString *docfilePath = [basePath stringByAppendingPathComponent:@"playerdata.plist"];
     NSFileManager *fileManager = [NSFileManager defaultManager];
     if (![fileManager fileExistsAtPath:docfilePath]){
         NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"playerdata" ofType:@"plist"];
         [fileManager copyItemAtPath:sourcePath toPath:docfilePath error:nil];
     }
     self.data = [NSMutableDictionary dictionaryWithContentsOfFile:docfilePath];
     return self;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:35:18+00:00Added an answer on June 10, 2026 at 6:35 pm

    The easiest way to create a dictionary from a plist is to use the method dictionaryWithContentsOfFile:,

    For example, to load a plist from your resources:

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"playerData" ofType:@"plist"];
    NSMutableDictionary *plistdict = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
    

    Writing a plistdict is equally simple:

    [plistdict writeToFile:filePath atomically:YES];
    

    Note that you can’t write into your app’s resources, so you’ll have to create a different path, e.g. in your Documents directory for your plist.

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    NSString *docfilePath = [basePath stringByAppendingPathComponent:@"playerData.plist"];
    [plistdict writeToFile:docfilePath atomically:YES];
    

    Now retrieve data from plist again.

     NSMutableDictionary *plistdict = [NSMutableDictionary dictionaryWithContentsOfFile:docfilePath];
    

    Add your content in plistDict and write it again.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making my first game in iOS and I need to load some levels
I am making an iOS game and I need a detection for both simple
Basically I need a suggestion about making an iOS application, where I can connect
my problem is: I'm making a game for iOS using cocos2d and this game
I am making an AIR for iOS app that can download pdfs. I do
Possible Duplicate: Determine device (iPhone, iPod Touch) with iOS I am making a game
I'm making an iOS application that has an interface in the portrait view. However,
I am making a 2d game with destructable terrain. It will be on iOS
I am making a small iOS game, and have finished everything, except the sound.
I'm making an iOS app - real-time game, wanna use UDP protocol. I'm searching

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.