Frage

Alright everyone, I've been working on a simple calculator tutorial for iOS as several friends of mine want me to teach them some rudimentary concepts. I think I've spent too much time doing fine-tuning on apps I've already written because when I wrote this code, it was plagued with errors.

I'll post the code; explanation of how it works will come after.

#import "_23ViewController.h"

@interface _23ViewController ()
{
    NSMutableArray* stack;
    NSMutableString *stringA;
    NSMutableString *stringB;
    NSMutableString *operation;
    NSUInteger *aorb;
}

@end

@implementation _23ViewController

- (void)viewDidLoad
{
   [super viewDidLoad];

   // Do any additional setup after loading the view, typically from a nib.
   [stack addObject: stringA];
   [stack addObject: stringB];
}

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

- (IBAction)zero:(id)sender
{
   [[stack objectAtIndex:*(aorb)] stringByAppendingString:@"0"];
}

- (IBAction)one:(id)sender
{
   [[stack objectAtIndex:*(aorb)] stringByAppendingString:@"1"];
}

- (IBAction)two:(id)sender
{
   [[stack objectAtIndex:*(aorb)] stringByAppendingString:@"2"];
}

- (IBAction)three:(id)sender
{
   [[stack objectAtIndex:*(aorb)] stringByAppendingString:@"3"];
}

- (IBAction)four:(id)sender
{
   [[stack objectAtIndex:*(aorb)] stringByAppendingString:@"4"];
}

- (IBAction)five:(id)sender
{
   [[stack objectAtIndex:*(aorb)] stringByAppendingString:@"5"];
}

- (IBAction)six:(id)sender
{
   [[stack objectAtIndex:*(aorb)] stringByAppendingString:@"6"];
}

- (IBAction)seven:(id)sender
{
   [[stack objectAtIndex:*(aorb)] stringByAppendingString:@"7"];
}

- (IBAction)eight:(id)sender
{
   [[stack objectAtIndex:*(aorb)] stringByAppendingString:@"8"];
}

- (IBAction)nine:(id)sender
{
   [[stack objectAtIndex:*(aorb)] stringByAppendingString:@"9"];
}

- (IBAction)decimal:(id)sender
{
   [[stack objectAtIndex:*(aorb)] stringByAppendingString:@"."];
}

- (IBAction)equals:(id)sender
{
   float a = [[stack objectAtIndex:0] floatValue];
   float b = [[stack objectAtIndex:1] floatValue];
   float answer;

   if ([operation isEqualToString:@"addition"])
   {
       answer = a + b;
   }
   if ([operation isEqualToString:@"subtraction"])
   {  
       answer = a - b;
   }
   if ([operation isEqualToString:@"multiplication"])
   {
       answer = a * b;
   }
   else
   {
       answer = a / b;
   }
   self.disp.text = [NSString stringWithFormat:@"%f",answer];
}

- (IBAction)clear:(id)sender
{
    for (int i = 0; i < 2; i++)
    {
        [stack insertObject:@"0" atIndex:i];
    }
}

- (IBAction)plus:(id)sender
{
    aorb++;
    [operation setString: @"addition"];
}

- (IBAction)minus:(id)sender
{
    aorb++;
    [operation setString: @"subtraction"];
}

- (IBAction)div:(id)sender
{
    aorb++;
    [operation setString: @"division"];
}

- (IBAction)mult:(id)sender
{
    aorb++;
    [operation setString: @"multiplication"];
}

@end

Now, ideally what this code is supposed to do is upon loading the view, add stringA and stringB to the array called stack. Every number that is pressed should append to the string located at the array index specified by the NSUInteger aorb. Initially this is zero, but when an operation button is pressed, it increments, and the operation NSString is set to the specific operation. Once the NSUInteger is incremented, the number keys append to stringB instead of stringA.The equals IBAction takes that into account and spins the result accordingly.

Problem is, whenever I simulate this, I get a EXC_BAD_ACCESS error, code 2, address 0x0 on whichever key I press first, specifically on the line that ideally performs. I had to debug a SIGABRT before, and nothing that helped that really works on this error. This is the first time I've thrown a memory exception like this, and so I'm really lost on what to do.

If this isn't enough detail/if you need more code (the .h file), let me know and I'll post it through. Thanks, everyone.

War es hilfreich?

Lösung

Add this to your viewDidLoad

[super viewDidLoad];

stack = [NSMutableArray array];
stringA = [NSMutableString string];
stringB = [NSMutableString string];

[stack addObject: stringA];
[stack addObject: stringB];

You also need to initialize aorb (and give it a better name). It also probably doesn't need to be a pointer.

Change it to NSUInteger aOrB; and add aOrB = 0 to viewDidLoad.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top