本文介绍了分配给模型类中的属性的值在视图/控制器类中消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的模型类发送变量 stringToDisplay 之前,NSLog向​​我显示它有一个值。但是当我尝试在我的ViewController中使用它时,我只得到(null)。关于我做错了什么的想法?

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

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

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

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

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)

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

有什么想法吗?

推荐答案

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

I can guess that problem lies in the way you assign your stringForDisplay, eg.:

如果您使用类似

stringForDisplay_ = anotherString;

属性的setter不会触发,所以你必须自己保留你的变量,否则它会活直到你的方法结束;

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;

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

that way ARC will do all the memory management.

这篇关于分配给模型类中的属性的值在视图/控制器类中消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 09:53