Issue with NSNumber in Xcode
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 fi开发者_如何转开发le, 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?
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.
精彩评论