0

私は、オンライン チュートリアル itunes.apple に基づいて電卓アプリを作成しています。com/itunes-u/ipad-iphone-application-development/ id473757255 (tut 2)

メソッド呼び出しの最後のステップまで、私はすべてのステップを綿密に追跡しましたが、すべて問題ありませんでした。ビルドして実行すると、数値と入力機能が正常に機能します。操作方法だけが機能していません。ということで、一番の問題は操作方法にあると思います。

BrainCalculator.h

@interface CalculatorBrain : NSObject

-(void) pushOperand: (double)operand;
-(double) performOperation: (NSString*) operation;


@end

BrainCalculator.m

#import "CalculatorBrain.h"
@interface CalculatorBrain()
@property (nonatomic, strong) NSMutableArray* _operandStack;

@end

@implementation CalculatorBrain
@synthesize _operandStack;
-(NSMutableArray *)operandStack
{
    if (!_operandStack){
        _operandStack= [[NSMutableArray alloc ]init];
    }

    return _operandStack;
}



-(void)pushOperand:(double)operand{
    NSNumber *operandObject = [NSNumber numberWithDouble:operand];
    [self.operandStack addObject:operandObject];
}

-(double)popOperand{
    NSNumber *operandObject= [self.operandStack lastObject];
    if (operandObject) [self.operandStack removeLastObject];    
    return [operandObject doubleValue];
}   


-(double)performOperation:(NSString *)operation
{
    double result = 0;

    if ([operation isEqualToString:@"+"]){
        result=[self popOperand] + [self popOperand];
    }else if ([@"*" isEqualToString:operation]){
        result = [self popOperand] * [self popOperand];
    }else if ([operation isEqualToString:@"-"]){
        double subtrahend = [self popOperand];
        result = [self popOperand] - subtrahend;
    }else if( [operation isEqualToString:@"/"]){
        double divisor = [self popOperand];
        if (divisor) result = [self popOperand] / divisor;
    }
    [self pushOperand:result];
    return result;
}

@end

最初は、performOperation メソッドがかなり怪しいと思われたので、いじってみました。

 }else if ([@"*" isEqualToString:operation]){

}else if ([operation isEqualToString:@"*"]){

うまくいくことを願っていましたが、残念ながらうまくいきませんでした。

追加情報のみ

viewcontroller.m

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

@interface CalculatorViewController ()
@property (nonatomic) BOOL userIsInTheMiddleOfEnteringANumber;
@property (nonatomic, strong) CalculatorBrain *brain;
@end

@implementation CalculatorViewController
@synthesize display;
@synthesize userIsInTheMiddleOfEnteringANumber;
@synthesize brain= _brain;  

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




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

    NSString * digit= [ sender currentTitle];
    if (userIsInTheMiddleOfEnteringANumber){
        self.display.text = [self.display.text stringByAppendingString:digit];
    }
    else{
        self.display.text=digit;
        self.userIsInTheMiddleOfEnteringANumber = YES;
    }

} 


- (IBAction)enterPressed {
    [self.brain pushOperand:[self.display.text doubleValue]];
    self.userIsInTheMiddleOfEnteringANumber = NO;  

}

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





@end

最終年度の主要プロジェクトに向けて準備するために xcode を練習しているので、助けていただければ幸いです。

4

0 に答える 0