在我的模型类发送变量之前stringToDisplay
, NSLog 告诉我它有一个值。但是当我尝试在我的 ViewController 中使用它时,我只是得到(null)
。对我做错了什么有什么想法吗?
(好消息是,在研究这个问题的过程中,我在理解模型和控制器如何相互关联方面取得了突破。我仍然是一个完全的新手,但我并不像以前那样感到失落。 )
这是我认为的相关代码:
计算器大脑.h
#import <Foundation/Foundation.h>
@interface CalculatorBrain : NSObject
@property (nonatomic) NSMutableString *stringToAdd;
@property (nonatomic,strong) NSString *stringForDisplay;
- (double)performOperation:(NSString *)operation withArray:(NSMutableArray *)particularStackYouNeedToPopOff;
计算器脑.m
@implementation CalculatorBrain
@synthesize stringToAdd = _stringToAdd;
@synthesize stringForDisplay = _stringForDisplay;
@synthesize whatHappenedSinceLastClear = _whatHappenedSinceLastClear;
- (double)performOperation:(NSString *)operation withArray:(NSMutableArray *)particularStackYouNeedToPopOff
{
<long code that I think doesn't matter because this NSLog produces exactly what I want it to:>
NSLog(@"%@",stringForDisplay);
return result;
}
CalculatorViewController.h
#import <UIKit/UIKit.h>
@interface CalculatorViewController : UIViewController
@property (nonatomic) NSArray *arrayOfDictionaries;
@property (nonatomic) NSDictionary *dictionary;
@property (weak, nonatomic) IBOutlet UILabel *variablesUsed;
@property (nonatomic, strong) NSString *operation;
@end
CalculatorViewController.m
#import "CalculatorViewController.h"
#import "CalculatorBrain.h"
@interface CalculatorViewController ()
@property (nonatomic,strong) CalculatorBrain *brain;
@end
@implementation CalculatorViewController
@synthesize display = _display;
@synthesize history = _history;
@synthesize brain = _brain;
@synthesize operation = _operation;
- (IBAction)operationPressed:(UIButton *)sender
{
NSString *otherString=[self.brain stringForDisplay];
if (self.userIsEnteringNumber) [self enterPressed];
NSString *operation = sender.currentTitle;
double result = [self.brain performOperation:operation withArray:[self.brain whatHappenedSinceLastClear]];
self.display.text = [NSString stringWithFormat:@"%g",result];
self.history.text = otherString;
NSLog(@"%@",otherString);
}
最后一行代码中的 NSLog 给了我(null)
.
有什么想法吗?