0

Right before my model class sends the variable stringToDisplay, NSLog shows me that it has a value. But when I try to use it in my ViewController, I just get (null). Any thoughts about what I'm doing wrong?

(The good news is that, while working on this, I had sort of a breakthrough in understanding how models and controllers relate to each other. I'm still a complete newbie, but I don't feel quite as lost as I did.)

Here's what I think is the relevant code:

CalculatorBrain.h

#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject
@property (nonatomic) NSMutableString *stringToAdd;
@property (nonatomic,strong) NSString *stringForDisplay;

- (double)performOperation:(NSString *)operation withArray:(NSMutableArray *)particularStackYouNeedToPopOff;

CalculatorBrain.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);
}

And the NSLog in that last line of code give me (null).

Any thoughts?

4

4 回答 4

2

也许我遗漏了一些东西,但是你的属性是在类扩展中声明的,CalculatorBrain所以外面没有人CalculatorBrain.m知道这个属性。

因此,如果您想将此属性公开给其他对象,则必须改为声明它CalculatorBrain.h

于 2012-08-01T17:19:43.447 回答
2

哦 - 您的属性声明whatHappenedSinceLastClear不会暴露给导入的其他类,CalculatorBrain.h因为您将属性声明放在​​文件的interface扩展名中.m,其他类不会看到。

要使其可公开访问,请移动to的@property行,而不是文件。whatHappenedSinceLastClearCalculatorBrain.h.m

于 2012-08-01T17:19:51.467 回答
1

我猜这个问题出在你分配你的方式上stringForDisplay,例如:

如果你使用类似的东西

stringForDisplay_ = anotherString;

setter for property 不会触发,因此您必须自己保留变量,否则它将一直存在直到您的方法完成;

如果是这样 - 使用属性设置器,例如:

self.stringForDisplay = anotherString;

这样 ARC 将完成所有的内存管理。

于 2012-08-02T07:06:58.203 回答
1

这实际上取决于您如何在方法中设置 stringForDisplay performOperation:withArray:

盲目猜测,请尝试使用

NSString *otherString = self.brain.stringForDisplay;

在这条线之后

double result = [self.brain performOperation:operation withArray:[self.brain whatHappenedSinceLastClear]];
于 2012-08-02T07:14:57.470 回答