简体   繁体   English

EXC_BAD_ACCESS,简单的计算器,Objective-C

[英]EXC_BAD_ACCESS, simple calculator, Objective-C

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.大家好,我一直在为 iOS 编写一个简单的计算器教程,因为我的几个朋友希望我教他们一些基本概念。 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 .现在,理想情况下,这段代码应该做的是在加载视图时,将stringAstringB添加到名为stack的数组中。 Every number that is pressed should append to the string located at the array index specified by the NSUInteger aorb .每个按下的数字都应附加到位于 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.最初这是零,但是当按下操作按钮时,它会增加,并将operation NSString 设置为特定操作。 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.一旦 NSUInteger 增加,数字键就会附加到stringB而不是stringAequals IBAction会考虑到这一点并相应地旋转结果。

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.问题是,每当我模拟这个时,我都会收到EXC_BAD_ACCESS error, code 2, address 0x0在我首先按下的任何键上的EXC_BAD_ACCESS error, code 2, address 0x0 ,特别是在理想执行的行上。 I had to debug a SIGABRT before, and nothing that helped that really works on this error.我之前必须调试SIGABRT ,但没有任何帮助可以真正解决此错误。 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.如果这还不够详细/如果您需要更多代码( .h文件),请告诉我,我会将其发布。 Thanks, everyone.谢谢大家。

Add this to your viewDidLoad将此添加到您的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).你还需要初始化aorb (并给它一个更好的名字)。 It also probably doesn't need to be a pointer.它也可能不需要是指针。

Change it to NSUInteger aOrB;将其更改为NSUInteger aOrB; and add aOrB = 0 to viewDidLoad .并将aOrB = 0添加到viewDidLoad

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM