问题
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?
回答1:
I can guess that problem lies in the way you assign your stringForDisplay
, eg.:
if you use something like
stringForDisplay_ = anotherString;
setter for property doesn't fire, so you have to retain your variable yourself otherwise it'll live just until your method finishes;
If so - use property setters, eg.:
self.stringForDisplay = anotherString;
that way ARC will do all the memory management.
回答2:
Maybe I'm missing something but your property is declared in the class extension of CalculatorBrain
so nobody outside CalculatorBrain.m
knows about this property.
So if you want to expose this property to other objects, you will have to declare it in CalculatorBrain.h
instead.
回答3:
Oh - your declaration of the property whatHappenedSinceLastClear
isn't exposed to other classes that import CalculatorBrain.h
because you put the property declaration in an interface
extension in the .m
file, which other classes will not see.
To make it publicly accessible move the @property
line for whatHappenedSinceLastClear
to CalculatorBrain.h
, not the .m
file.
回答4:
It really depends how you set stringForDisplay inside the performOperation:withArray:
method.
for a blind guess, try using
NSString *otherString = self.brain.stringForDisplay;
after this line
double result = [self.brain performOperation:operation withArray:[self.brain whatHappenedSinceLastClear]];
来源:https://stackoverflow.com/questions/11772307/value-assigned-to-a-property-in-the-model-class-disappears-in-the-view-controlle