0

モデルクラスが変数を送信する直前にstringToDisplay、NSLogはそれが値を持っていることを示します。しかし、ViewControllerで使用しようとすると、。が表示されます(null)。私が間違っていることについて何か考えはありますか?

(幸いなことに、これに取り組んでいる間、モデルとコントローラーが互いにどのように関連しているかを理解する上で、ある種のブレークスルーがありました。私はまだ完全な初心者ですが、私ほど迷うことはありません。 )。

関連するコードは次のとおりです。

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

そして、その最後のコード行のNSLogは私に与えます(null)

何かご意見は?

4

4 に答える 4

2

何かが足りないかもしれませんが、あなたのプロパティはのクラス拡張で宣言されているCalculatorBrainので、外部の誰もCalculatorBrain.mこのプロパティについて知りません。

したがって、このプロパティを他のオブジェクトに公開する場合は、CalculatorBrain.h代わりにで宣言する必要があります。

于 2012-08-01T17:19:43.447 に答える
2

ああ-プロパティ宣言をファイルの拡張子に入れたため、プロパティの宣言はwhatHappenedSinceLastClearインポートされる他のクラスに公開されません。他のクラスには表示されません。CalculatorBrain.hinterface.m

公開してアクセスできるようにするには、ファイルではなく、の@property行をwhatHappenedSinceLastClearに移動します。CalculatorBrain.h.m

于 2012-08-01T17:19:51.467 に答える
1

私はあなたがあなたを割り当てる方法に問題があると推測することができますstringForDisplay、例えば:

あなたがのようなものを使用する場合

stringForDisplay_ = anotherString;

プロパティのセッターは起動しないため、変数を自分で保持する必要があります。そうしないと、メソッドが終了するまで変数が存続します。

その場合-プロパティセッターを使用します。例:

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 に答える