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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:15:24+00:00 2026-05-31T11:15:24+00:00

Newbie error? Im working on a quiz app that uses answer results from a

  • 0

Newbie error?

Im working on a quiz app that uses answer results from a view contoller, creates NSMutableArray objects for totalCorrect and totalTried values in a KeepScore model .m file and the model returns a score back to my view controller. The view controller then updates my score label in my view. I then use the UINavigationController to segue to my next view controller and then wait for the user to input the next quiz question answer. Problem is that the next time i call my KeepScore model .m file from my view controller 2, the NSMutableArray objects in KeepScore model .m have reverted to Null values. I made sure my NSMutableArray object values were correct (non Null) right before returning from my KeepScore .m model to my view controller 1. Im working iOS 5.1 with Xcode 4.3.1 with ARC. My NSMutableArray property in my private API declaration in .m model file is strong. Shouldn’t the KeepScore model .m keep my array object values when i return to my view controller so that i can use them again when i call my model from my next view controller? I wanted to ask in wording first to see if anyone catches any syupid logic I have. I’ll post code if helpful.

Here is my KeepScore.h model
#import

@interface KeepScore : NSObject

- (float)score:(BOOL)questionCorrect:(BOOL)questionTotal:(BOOL)firstScoringPass;
@end

Here is my KeepScore.m model implementation:

#import "KeepScore.h"

@interface KeepScore()
@property (nonatomic, strong) NSMutableArray *correctScoreValues;
@end

@implementation KeepScore

@synthesize correctScoreValues = _correctScoreValues;

- (NSMutableArray *)correctScoreValues
{
    if (_correctScoreValues == nil) _correctScoreValues = [[NSMutableArray alloc] init];
    return _correctScoreValues;
}

- (float)score:(BOOL)questionCorrect:(BOOL)questionTotal:(BOOL)firstScoringPass {

int totalTried;
int totalCorrect;
NSLog(@"firstScoringPass = %@", firstScoringPass ? @"YES" : @"NO");
NSLog(@"questionCorrect = %@", questionCorrect ? @"YES" : @"NO");
NSLog(@"questionTotal = %@", questionTotal ? @"YES" : @"NO");
if (firstScoringPass) {
    int totalTriedInt = 0;
    int totalCorrectInt = 0;
    NSNumber *correct = [NSNumber numberWithInt:totalCorrectInt];
    NSNumber *tried = [NSNumber numberWithInt:totalTriedInt];
    [self.correctScoreValues addObject:correct];
    [self.correctScoreValues addObject:tried];
    firstScoringPass = NO;
}

if (questionCorrect) {
    totalCorrect = ([[self.correctScoreValues objectAtIndex:0] intValue] + 1);
    NSLog(@"totalCorrect = %d", totalCorrect);
    NSNumber *correct = [NSNumber numberWithInt:totalCorrect];
    NSLog(@"correct = %@", correct);
    [self.correctScoreValues replaceObjectAtIndex:0 withObject:correct];
    NSLog(@"correct value in array = %d",[[self.correctScoreValues objectAtIndex:0] intValue]);
} else {
    totalCorrect = [[self.correctScoreValues objectAtIndex:0] intValue];
}

NSLog(@"totalCorrect = %d", totalCorrect);
if (questionTotal) {
    NSLog(@"tried value in array = %d", [[self.correctScoreValues objectAtIndex:1] intValue]);
    totalTried  = ([[self.correctScoreValues objectAtIndex:1] intValue] + 1);
    NSNumber *tried = [NSNumber numberWithInt:totalTried];
    [self.correctScoreValues replaceObjectAtIndex:1 withObject:tried];
    NSLog(@"tried value in array = %d", [[self.correctScoreValues objectAtIndex:1] intValue]);
}

float score = (totalCorrect/totalTried);
NSLog(@"score = %0.2f", score);
NSLog(@"totalCorrect = %d, totalTried = %d", totalCorrect, totalTried);

NSLog(@"correct value in array = %d", [[self.correctScoreValues objectAtIndex:0] intValue]);
NSLog(@"tried value in array = %d", [[self.correctScoreValues objectAtIndex:1] intValue]);

return score;
}
@end

Here is one of my ViewControllers .m files:

#import "MapQuizViewController.h"
#import "KeepScore.h"

@interface MapQuizViewController()
@property (nonatomic, strong) KeepScore *scoreModel;
@property (nonatomic) BOOL questionTotal;
@property (nonatomic) BOOL questionCorrect;
@property (nonatomic) BOOL firstScoringPass;
@end

@implementation MapQuizViewController
@synthesize gradeLabel = _gradeLabel;
@synthesize scoreLabel = _scoreLabel;

@synthesize scoreModel = _scoreModel;
@synthesize questionTotal = _questionTotal;
@synthesize questionCorrect = _questionCorrect;
@synthesize firstScoringPass = _firstScoringPass;

- (KeepScore *)scoreModel;
{
    if (!_scoreModel) _scoreModel = [[KeepScore alloc] init];
    return _scoreModel;
}

- (IBAction)answerButtonPressed:(UIButton *)sender 
{

NSString *answerChoice = sender.currentTitle;
NSString *correct = [NSString stringWithFormat:@"Correct!"];
NSString *incorrect = [NSString stringWithFormat:@"Incorrect"];
self.questionTotal = YES;
self.firstScoringPass = YES;
if ([answerChoice isEqualToString:@"Australia"]) {
    self.gradeLabel.textColor = [UIColor blueColor];
    self.gradeLabel.text = [NSString stringWithFormat:@"%@",correct];
    self.questionCorrect = YES;
    self.scoreLabel.text = [NSString stringWithFormat:@"%0.2f",[self.scoreModel score:_questionCorrect :_questionTotal :_firstScoringPass]];
} else if ([answerChoice isEqualToString:@"U.S."]) {
    self.gradeLabel.textColor = [UIColor redColor];
    self.gradeLabel.text = [NSString stringWithFormat:@"%@", incorrect];
    self.questionCorrect = NO;        
    self.scoreLabel.text = [NSString stringWithFormat:@"%0.2f",[self.scoreModel score:_questionCorrect :_questionTotal :_firstScoringPass]];
} else if ([answerChoice isEqualToString:@"China"]) {
    self.gradeLabel.textColor = [UIColor redColor];
    self.gradeLabel.text = [NSString stringWithFormat:@"%@", incorrect];
    self.questionCorrect = NO;        
    self.scoreLabel.text = [NSString stringWithFormat:@"%0.2f",[self.scoreModel score:_questionCorrect :_questionTotal :_firstScoringPass]];
}
}
- (void)viewDidUnload {
  [self setScoreLabel:nil];
   [super viewDidUnload];
  }
@end
  • 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-05-31T11:15:25+00:00Added an answer on May 31, 2026 at 11:15 am

    First, a single .m file means nothing. I think you assumed that there is one instance of a Class per .m file which is not true.

    for example

    // in .m file
    static int *myInt; // only one `myInt` exist
    
    // in .h or .m file
    @interface MyClass : NSObject {
        int myVar;    // each MyClass instance will have its own `myVar` which are different
    }
    
    // somewhere else
    MyClass *a = [[MyClass alloc] init];
    MyClass *b = [[MyClass alloc] init];
    a->myVar = 1;
    b->myVar = 2;
    // a->myVar is 1 and b->myVar is 2
    

    If you didn’t pass your KeepScore instance around, newly created one will not have previous one’s data.

    You have to either to make KeepScore singleton or pass it from old view controller to new view controller

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

Sidebar

Related Questions

Newbie question. I have a NSMutableArray that holds multiple objects (objects that stores Bezier
I am working on an app where users' projects can follow plant objects from
I am a newbie with a problem working with Django-nonrel on Google App Engine.
I'm working on learning some Haskell (please excuse the newbie error)- This routine errors
I'm a newbie in boost.python and i'm getting this error that I would like
I'm a newbie to iphone development and I've been struggling with an EXC_BAD_ACCESS error
Because I am a newbie I am trying to log out any errors that
Newbie question: I just installed VisualSVN Server and created a repository. I noticed that
newbie for clearcase. Since clearcase's config is rather different from other concept in git,
I'm a newbie Rails developer who is getting the following error when trying to

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.