1

私はプログラム可能な電卓に取り組んでいますが、私の人生では、何が間違っているのか理解できません。

コードの関連部分を次に示します。(コードは未完成なので、余分なものが浮かんでいることはわかっています。)

CalculatorViewController.m

#import "CalculatorViewController.h"
#import "CalculatorBrain.h"

@interface CalculatorViewController ()
@property (nonatomic) BOOL userIsEnteringNumber;
@property (nonatomic) BOOL numberIsNegative;
@property (nonatomic,strong) CalculatorBrain *brain;
@property (nonatomic) NSArray *arrayOfDictionaries;
@property (nonatomic) NSDictionary *dictionary;
@end

@implementation CalculatorViewController
@synthesize display = _display;
@synthesize history = _history;
@synthesize userIsEnteringNumber = _userIsEnteringNumber;
@synthesize numberIsNegative;
@synthesize brain = _brain;
@synthesize arrayOfDictionaries;
@synthesize dictionary;

-(CalculatorBrain *)brain
{
if (!_brain) _brain = [[CalculatorBrain alloc] init];
return _brain;
}

/*snip code for some other methods*/

- (IBAction)variablePressed:(UIButton *)sender 
{
NSString *var = sender.currentTitle; 
NSDictionary *dict = [self.dictionary initWithObjectsAndKeys:[NSNumber numberWithDouble:3],@"x",[NSNumber numberWithDouble:4.1],@"y",[NSNumber numberWithDouble:-6],@"z",[NSNumber numberWithDouble:8.7263],@"foo",nil];
[self.brain convertVariable:var usingDictionary:dict];
self.display.text = [NSString stringWithFormat:@"%@",var];
self.history.text = [self.history.text stringByAppendingString:sender.currentTitle];
[self.brain pushOperand:[dict objectForKey:var] withDictionary:dict];
}

@end

そして、これが CalculatorBrain.m です。

#import "CalculatorBrain.h"

@interface CalculatorBrain ()
@property (nonatomic, strong) NSMutableArray *operandStack;
@end

@implementation CalculatorBrain
@synthesize operandStack = _operandStack;


-(void)pushOperand:(id)operand withDictionary:(NSDictionary *)dictionary
{
NSNumber *operandAsObject;
if (![operand isKindOfClass:[NSString class]])
{
    operandAsObject = operand;
}
else 
{
    operandAsObject = [dictionary objectForKey:operand];
}
[self.operandStack addObject:operandAsObject];
}

-(double)popOperand
{
NSNumber *operandAsObject = [self.operandStack lastObject];
if (operandAsObject) [self.operandStack removeLastObject];
return [operandAsObject doubleValue];
}

-(double)convertVariable:(NSString *)variable usingDictionary:dictionary
{
double convertedNumber = [[dictionary objectForKey:variable] doubleValue];
return convertedNumber;
}

@end

私が理解に苦しんでいるのはCalculatorViewController.m方法- (IBAction)variablePressed:(UIButton *)senderです。この行はプログラムをクラッシュさせます:

NSDictionary *dict = [self.dictionary initWithObjectsAndKeys:[list of objects and keys]];

でも出来たら

NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:[list of objects and keys]];

その後、すべて正常に動作します。でもやろうとしたら

NSDictionary *dict = [[self.dictionary alloc] initWithObjectsAndKeys:[list of objects and keys]];

これは私には正しいことのように思えますが、XCode では許可されないため、明らかに何かを理解していません。

何かご意見は?

4

1 に答える 1

3

+allocオブジェクトにメモリを割り当てます。-init...メソッドはオブジェクトを初期化します。

[self.dictionary initWithObjectsAndKeys:...そのプロパティまたはnilに設定された辞書を返す呼び出しを呼び出してから、それ-dictionaryを呼び出そうとしますinit...。ディクショナリが存在する場合は、無効なオブジェクトを複数回初期化しようとしています。プロパティが設定されていない場合、getter は nil を返し、nil にinit...メッセージを送信しても何もしません。いずれにせよ、これはあなたがやりたいことではありません。

[[self.dictionary alloc] init...コンパイラが警告するように、これも無効です。ここで、からオブジェクトを取得して、-dictionaryそのクラス メソッドを呼び出します+alloc

ここでは、オブジェクトの作成方法とプロパティ アクセサーの機能について、根本的な混乱が生じているようです。object creationdot syntaxを見ることを提案する以外に、それに対処する方法がわかりません。

于 2012-08-01T01:43:15.690 に答える