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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:54:01+00:00 2026-05-22T17:54:01+00:00

I’m trying to make a guessing game for the iPad, and I’m using an

  • 0

I’m trying to make a guessing game for the iPad, and I’m using an NSNumber to keep track of the number of tries the user has left in the game, called numberOfTries. The NSNumber is in a view-based file, and I first initialize it in the drawRect method, in order to draw boxes indicating how many tries the user has left. I initialize the number at an int value of 5, and it works at that point, but when I try to use it in another method within the class, like one that indicates whether the user is out of tries, it shows that it’s already at zero by the time I enter the first guess. Here’s the code:

//Game.h
#import <UIKit/UIKit.h>


@interface Game : UIView {
    NSString* equation;
    NSNumber* numberOfTries;
}

@property(nonatomic, retain)NSString* equation;
@property(nonatomic, retain)NSNumber* numberOfTries;

@end


//Game.m
#import "Game.h"


@implementation Game

@synthesize equation, numberOfTries;

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}

-(BOOL)hasLost
{
    int tryNumber = [self.numberOfTries intValue];
    return tryNumber <= 0;
}

-(BOOL)guessWithEquation:(NSString*)guessEquation
{
    int tryNumber = [self.numberOfTries intValue];
    if ([self.equation isEqualToString:guessEquation]) {return YES;}
    tryNumber--;
    self.numberOfTries = [NSNumber numberWithInt:tryNumber];
    return NO;
}

-(void)setEquationTo:(NSString*)theEquation {self.equation = [[NSString alloc] initWithString:theEquation];}

- (void)drawRect:(CGRect)rect {
    self.numberOfTries = [[NSNumber alloc] initWithInt:5];
    int halfwidth = self.bounds.size.width/2;
    int halfheight = self.bounds.size.height/2;
    int threeqw = self.bounds.size.width-(self.bounds.size.width/4);
    int threeqh = self.bounds.size.height-(self.bounds.size.height/4);
    int oneqw = self.bounds.size.width/4;
    int oneqh = self.bounds.size.height/4;

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2.0);
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    CGFloat components[] = {0.0, 0.0, 0.0, 1.0};
    CGColorRef color = CGColorCreate(colorspace, components);
    CGContextSetStrokeColorWithColor(context, color);

    int tryNumber = [self.numberOfTries intValue];
    int point = oneqw-100;
    while (tryNumber > 0) {
        CGContextMoveToPoint(context, point, threeqh);
        CGContextAddLineToPoint(context, point+15, threeqh);
        CGContextAddLineToPoint(context, point+15, threeqh+15);
        CGContextAddLineToPoint(context, point, threeqh+15);
        CGContextAddLineToPoint(context, point, threeqh);
        tryNumber--;
        point += 30;
    }

    CGContextStrokePath(context);
    CGColorSpaceRelease(colorspace);
    CGColorRelease(color);
}

- (void)dealloc {
    [super dealloc];
}


@end

When it calls guessWithEquation, numberOfTries is already at 0, which is really confusing. What am I missing here?

  • 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-22T17:54:01+00:00Added an answer on May 22, 2026 at 5:54 pm

    You start out with numberOfTries uninitialised, and hence (because it is an instance variable and this is how Objective-C works) nil. Then, every time you redraw, you initialise numberOfTries to be a new NSNumber with value 5. And you throw in a spurious retain for good measure. How do you expect that to end well?

    Initialise when you start, possibly in initWithFrame though you may also need to consider awakeFromNib if the view is loaded from an IB resource file. From then on, replace the value when it changes but do not recreate the object for no good reason.

    And FFS never never never do this: [self.something retain]. What do you think that is doing? The property is already defined as retain. Trust that. This sort of thing is just making a mess.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

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.