2015-09-20 58 views
-4

我正在寫一個計算器應用程序在objective-c爲iOS。此時我的目標是將用戶按下的數字添加到充當LIFO堆棧的NSMutableArray中,以便稍後可以通過將它們從堆棧彈出來對其執行計算。我在'(IBAction)operationPressed'方法的末尾插入了一個斷點,調試器顯示我的NSMutableArray aka programStack的值爲'nil'。任何人都可以看到我在這裏做錯了嗎?添加對象到NSMutableArray,但得到零而不是

programStack在我的.m文件合成,並宣佈爲非原子,強於我的.h文件中

讓我知道如果有更多的代碼,我應該顯示

 //All code from my .h file 
     #import <UIKit/UIKit.h> 
     /* 
     @interface ViewController : UIViewController 


     @end 
     */ 

     @interface Compute : NSObject 
     @property (nonatomic, strong) NSMutableArray *programStack; 

     -(void)pushOperand:(double)operand; 
     -(double)performOperation:(NSString *)operation; 
     -(double)popOperand; 
     @end 

     @interface Calculator : UIViewController 
     @property (nonatomic, strong) Compute *comp; 
     @property (nonatomic, weak) NSString *operationUserHasPressed; 
     @property (strong, nonatomic) IBOutlet UITextField *display; 
     @property double operand; //I added this 

     -(IBAction)digitPressed:(UIButton *)sender; 
     -(IBAction)operationPressed:(UIButton *)sender; 
     -(IBAction)equalToSignPressed:(UIButton *)sender; 
     @end 

     //Some code from my .m file 
     #import "ViewController.h" 
     @interface Compute() 

     @end 

     @implementation Compute 

     @synthesize programStack; 

     -(void)pushOperand:(double)operand { 
      [self.programStack addObject: [NSNumber numberWithDouble:operand]]; 
     } 
     @end 
     @interface Calculator() 

     @end 

     @implementation Calculator 

     - (void)viewDidLoad { 
      [super viewDidLoad]; 
      // Do any additional setup after loading the view, typically from a nib. 
      //An error comes up 
      self.programStack = [[NSMutableArray init] alloc]; 
     } 

     - (void)didReceiveMemoryWarning { 
      [super didReceiveMemoryWarning]; 
      // Dispose of any resources that can be recreated. 
     } 

     @synthesize display; 
     @synthesize operationUserHasPressed; 
     @synthesize comp; 

     -(IBAction)digitPressed:(UIButton *)sender { 

      //To obtain the number entered by the user 
      NSString *numberEntered = sender.currentTitle; 

      //Display number entered by the user 
      display.text = numberEntered; 

      if ([sender.currentTitle isEqual: @"1"]) { 
       self.operand = 1.00; 
      } else if ([sender.currentTitle isEqual:@"2"]) { 
       self.operand = 2.00; 
      } else if ([sender.currentTitle isEqual:@"3"]) { 
       self.operand = 3.00; 
      } else if ([sender.currentTitle isEqual:@"4"]) { 
       self.operand = 4.00; 
      } else if ([sender.currentTitle isEqual:@"5"]) { 
       self.operand = 5.00; 
      } else if ([sender.currentTitle isEqual:@"6"]) { 
       self.operand = 6.00; 
      } else if ([sender.currentTitle isEqual:@"7"]) { 
       self.operand = 7.00; 
      } else if ([sender.currentTitle isEqual:@"8"]) { 
       self.operand = 8.00; 
      } else if ([sender.currentTitle isEqual:@"9"]) { 
       self.operand = 9.00; 
      } 
     } 

     -(IBAction)operationPressed:(UIButton *)sender { 

      //To obtain the operation entered by the user 
      self.operationUserHasPressed = sender.currentTitle; 

      [self.comp pushOperand: self.operand]; 
     } //Place breakpoint here 

     -(IBAction)equalToSignPressed:(UIButton *)sender { 

      //To obtain the result of the computation 
      double result = 0; 
      [self.comp pushOperand: self.operand]; 
      result = [self.comp performOperation:self.operationUserHasPressed]; 

     } 
     @end 
+3

你應該你的代碼添加到您的問題,而不是屏幕截圖。你真的分配並初始化你的NSMutableArray? – Paulw11

回答

-1

您需要對代碼做一些小的修改。所有的 先用下面的代碼替換您的-(void)viewDidLoad方法:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // Allocation and Initialization of your Compute Class 
    comp = [[Compute alloc] init]; 
} 

而在你Compute.m文件添加一個方法:

- (id)init { 
    if(self == [super init]) { 
     self.programStack = [[NSMutableArray alloc] init]; 
    } 
    return self; 
} 
+0

mehulsojitra,teos提到它在init中的內部括號和alloc在外部的。它們可以互換嗎? –

+0

你能回答我的問題嗎? –

+0

我不確定,但我認爲你不能使用'self.programStack = [[NSMutableArray init] alloc];'。我試過了,應用崩潰了,所以使用上面說的代碼。 –

0

你提到'programStack'是合成的,但它只意味着它的公共getter/setter將被生成。它不會爲你創建。正如Paulw11在他的評論中指出的那樣,我相信你可能會錯過分配。

它通常發生在你的UIViewController的 - (id)init或 - (void)viewDidLoad中。

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.programStack = @[].mutableCopy; // or [[NSMutableArray init] alloc]; 
} 
+0

我沒有這樣做,謝謝! –

+0

所以我很困惑你有多少個UIViewControllers。當我啓動該項目時,我已經設置了默認的ViewController.m和.h。但是我的教授給了我們一些代碼,並且她將Calculator類定義爲UIViewController。我嘗試在 - (void)viewDidLoad下初始化默認的UIController中的programStack,但是我得到一個錯誤,所以我在我的.h文件中評論了ViewController,並且沒有在上面的.m部分中包含實現。相反,我將這些方法複製到Calculator UIViewController中。我錯了嗎? –

+0

如果這解決了您最初的問題,請考慮將其標記爲已解決。關於你的其他問題,你可以在應用中使用任何數量的viewController。如果你有問題想清楚他們的行爲,我建議你發表另一個(新)問題。 – teos