Usar una propiedad como conteo en Objective C

I am new to Objective C. I was following Stanford lectures 2011-12 fall on iOS development and in assignment 1 it asks to implement a decimal point in a calculator. This is what my implementation looks like:

#import "CalculatorViewController.h"
#import "CalculatorBrain.h"

@interface CalculatorViewController()

@property (nonatomic) BOOL userIsInTheMiddleOfEnteringNumber;
@property (nonatomic,strong) CalculatorBrain *brain;
@property (nonatomic) int userPressedDecimalPoint;
@end

@implementation CalculatorViewController
@synthesize display = _display;
@synthesize userIsInTheMiddleOfEnteringNumber = _userIsInTheMiddleOfEnteringNumber;
@synthesize userPressedDecimalPoint = _userPressedDecimalPoint; 
@synthesize brain = _brain;


- (CalculatorBrain *) brain{
if (!_brain) _brain = [[CalculatorBrain alloc] init];
return _brain;
}

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

NSString *digit = sender.currentTitle;
if (digit == @"."){
    self.userIsInTheMiddleOfEnteringNumber = YES;
    self.userPressedDecimalPoint++;
}

if (self.userIsInTheMiddleOfEnteringNumber &&  self.userPressedDecimalPoint< 2){
    self.display.text = [self.display.text stringByAppendingString:digit];
    NSLog(@"decimal pressed: %d times",self.userPressedDecimalPoint);
} 
else{
    self.display.text = digit;
    self.userIsInTheMiddleOfEnteringNumber = YES;
    self.userPressedDecimalPoint = 1;
}
}

So basically I have setup a property userPressedDecimalPoint and i try to use this property as a counter every time the decimal point is pressed. However from the NSLog i see that no matter how many times i press the decimal point it only shows 1 time pressed. Consequently, the output shows multiple decimal points if entered. Any suggestions would be appreciated. Thanks!

Full version of Corrected Code:

#import "CalculatorViewController.h"
#import "CalculatorBrain.h"

@interface CalculatorViewController()

@property (nonatomic) BOOL userIsInTheMiddleOfEnteringNumber;
@property (nonatomic,strong) CalculatorBrain *brain;
@property (nonatomic) int userPressedDecimalPoint;
@end

@implementation CalculatorViewController
@synthesize display = _display;
@synthesize userIsInTheMiddleOfEnteringNumber = _userIsInTheMiddleOfEnteringNumber;
@synthesize userPressedDecimalPoint = _userPressedDecimalPoint;
@synthesize brain = _brain;


- (CalculatorBrain *) brain{
if (!_brain) _brain = [[CalculatorBrain alloc] init];
return _brain;
}

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

NSString *digit = sender.currentTitle;
if ([digit isEqualToString:@"."]){
    self.userIsInTheMiddleOfEnteringNumber = YES;
    self.userPressedDecimalPoint++;
}

if (self.userIsInTheMiddleOfEnteringNumber &&  self.userPressedDecimalPoint< 2){
    self.display.text = [self.display.text stringByAppendingString:digit];
   // NSLog(@"decimal pressed: %d times",self.userPressedDecimalPoint);
} 
else if (![digit isEqualToString:@"."]){
    self.display.text = digit;
    self.userIsInTheMiddleOfEnteringNumber = YES;
}
}

- (IBAction)operationPressed:(UIButton *)sender {
if (self.userIsInTheMiddleOfEnteringNumber) [self enterPressed];
double result = [self.brain performOperation:sender.currentTitle];
NSString *resultString = [NSString stringWithFormat:@"%g", result];
self.display.text = resultString;

}

- (IBAction)enterPressed {
self.userPressedDecimalPoint = 0;
[self.brain pushOperand:[self.display.text doubleValue]];
self.userIsInTheMiddleOfEnteringNumber = NO;
} 

@end

preguntado el 29 de julio de 12 a las 16:07

1 Respuestas

Unless there is something that sets userIsInTheMiddleOfEnteringNumber a NO, any time the number is greater than one, your code will execute self.userPressedDecimalPoint = 1; and set it back to one.

Respondido 29 Jul 12, 16:07

OK when i removed self.userPressedDecimalPoint = 1 line the NSLog output now shows 0 no matter how many times i press the decimal point - stud91

Además, no deberías usar == to compare strings; try isEqualToString: en lugar de. (== compares addresses, not content.) Try stepping through the method in the debugger to see if it goes where you expect. - molinos phillip

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.