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 9046861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:45:34+00:00 2026-06-16T11:45:34+00:00

I try to make a simple score system for my game and I’m using

  • 0

I try to make a simple score system for my game and I’m using dictionary and save it using NSUserDefaults to store player’s scores. However when I try to set new score to existing key/value the app crash, there isn’t any console log message and xcode switches to the unreadable for me assembler code. I know where it crashes because I set exception and it shows on which line it has been called. I also use cocos2d if that change anything.

here’s the code:

//creates key for the right level     
NSString *currentLevelString = [NSString stringWithFormat:@"Level%i",numberOfCurentLevel];


 //checking if dictionary is empty, if empty then it means the game is played for the first time
 NSMutableDictionary *dictionary = [[[NSMutableDictionary alloc]init]autorelease];
//ud is the UserDefaults

 dictionary = [ud objectForKey:@"dictionaryWithScores"];

 if ([dictionary objectForKey:@"Level5"] == nil) {
     //assigning first values for empty dictionary
     dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"ScoreList" ofType:@"plist"]];
     NSLog(@"dictionary was empty");
 }
 //NSLog(@"now dictionary shoud not be empty %@",dictionary);
 //calculating current score for this level
 double currentScore = (numberOfCurentLevel * elementsFound)/9.0;///timeInSeconds;
 double previousScore = [[dictionary objectForKey:currentLevelString]doubleValue];//taking previous score for thi level
 NSLog(@"currentScore: %f \n previousScore: %f",currentScore,previousScore);
 //checking whether current score is greater than the previous one
 if (currentScore > previousScore)
 {
     NSLog(@"dictionary just before the crash: %@",dictionary);
     //if new score is greater then update the dictionary
     NSString *currentScoreString = [NSString stringWithFormat:@"%f",currentScore];

//it crashes on this line seObject

     [dictionary setObject:currentScoreString forKey:currentLevelString];
     [ud setObject:dictionary forKey:@"dictionaryWithScores"];
     [ud synchronize];

//calculating total score (score for each level + next one)
 double overallScore = 0;
 for (int i=1; i <= 101; i++) {
     //adding scores for each level to each other
     overallScore = overallScore + [[dictionary objectForKey:[NSString stringWithFormat:@"Level%i",i]]doubleValue];
    // NSLog(@"overal score: %f",overallScore);
 }

when I comment the setObject line then everything works fine. Can anyone if there is something that cause the crash? Thank you in advance.

  • 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-16T11:45:35+00:00Added an answer on June 16, 2026 at 11:45 am

    Your problem is this line:

    dictionary = [ud objectForKey:@"dictionaryWithScores"];
    

    That is returning an immutable dictionary, so there is no setObject: method. You loaded the plist file into a mutable dictionary, but all dictionaries inside that dictionary will still be immutable.

    To convert an immutable dictionary to a mutable one, do this:

    dictionary = [[ud objectForKey:@"dictionaryWithScores"] mutableCopy];
    

    You probably also want to save the mutable copy you just made back into the ud dictionary:

    [ud setObject:dictionary forKey:@"dictionaryWithScores"];
    

    PS: I’m not sure why you don’t see a log message; You should see one.

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

Sidebar

Related Questions

I'm trying to make a simple 2D game using SFML and this tutorial. Here's
I'm pretty new to PHP, but I decided to try and make a simple
I'll try to make this as simple as possible. I'm working on a site
Alright, I'll try and make this as simple as possible. I'm trying to create
I wanted to make some simple file recovery software, where I want to try
Ok, so I'll try to make that question a little more simple. The core
New to batch files, first try actually. Trying to make a simple batch file
I'm trying to make simple many-to-one association, using NHibernate.. I have class Recruit with
I'm using redis in my python application to store simple values like counters and
need help, I try to cut complexity and make simple example: <div id=tree_div> <ul>

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.