好的,我一直在研究他們發佈的免費在線的斯坦福大學iOS開發課程。我一直在研究如何編寫可編程變量。到目前爲止,它一直在正常工作,除了我認爲以下代碼行將變量編程爲@「x =」而不是前面輸入的數字。Objective C計算器編程變量
視圖控制器:
#import "ViewController.h"
#import "CalculatorBrain.h"
@interface ViewController()
@property (nonatomic) BOOL userIsInTheMiddleOfEnteringANumber;
@property (nonatomic) BOOL userPressedSomethingElse;
@property (nonatomic, strong) CalculatorBrain *brain;
@end
@implementation ViewController
@synthesize display;
@synthesize inputHistory;
@synthesize userPressedSomethingElse;
@synthesize userIsInTheMiddleOfEnteringANumber;
@synthesize brain = _brain;
- (CalculatorBrain *)brain
{
if (!_brain) _brain = [[CalculatorBrain alloc] init];
return _brain;
}
NSString *xValue = @"0";
- (IBAction)enterPressed
// A specific action if enter is pressed
{
[self.brain pushOperand:[self.display.text doubleValue]];
self.userIsInTheMiddleOfEnteringANumber = NO;
if (self.userPressedSomethingElse)
{
self.inputHistory.text = [self.inputHistory.text stringByAppendingString:@" "];
}
self.userPressedSomethingElse = NO;
}
- (IBAction)variableChanged:(id)sender
{
if (self.userIsInTheMiddleOfEnteringANumber)
{
[self enterPressed];
}
NSString *operation = [sender currentTitle];
xValue = [self.brain programVariable:operation];
self.inputHistory.text = [self.inputHistory.text stringByAppendingString:@"X="];
self.inputHistory.text = [self.inputHistory.text stringByAppendingString:xValue];
}
計算器腦(在.M一個):
#import "CalculatorBrain.h"
@interface CalculatorBrain()
@property (nonatomic,strong) NSMutableArray *operandStack;
@end
@implementation CalculatorBrain
@synthesize operandStack = _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];
}
- (NSString *) programVariable: (NSString *) operation
{
double result = [self popOperand];
NSString *resultString = [NSString stringWithFormat:@"%.2d",result];
return resultString;
}
的.H計算器大腦:
#import <Foundation/Foundation.h>
@interface CalculatorBrain : NSObject
- (void) pushOperand: (double) operand;
- (double) performOperation: (NSString *) operation;
- (NSString *) programVariable: (NSString *) operation;
@end
該推按鈕說「x =」,並且由於我添加了一些跟蹤語句,我已經發現它已被設置爲xValue。但是,我不知道如何解決它...任何想法?
你寫了@ @synthesize brain = _brain;'你爲什麼還寫self.brain?你的日誌顯示了什麼? – 2012-02-14 15:38:02
日誌行?那些是什麼? – BrainInaJar1245 2012-02-16 15:30:26
我的意思是你的追蹤報表。他們展示了什麼? – 2012-02-16 15:46:39