我的计算器应用程序崩溃并显示以下错误消息:libc++abi.dylib: terminating with uncaught exception of type NSException

My calculator app crashes with this error message: libc++abi.dylib: terminating with uncaught exception of type NSException

我对 Xcode 和 iOS 编程世界完全陌生。我第一次学习它是通过一个简单的计算器应用程序。一切正常,直到我通过将 calculatordisplay class 重命名为 calculatorDisplay(大写字母 D)对我的 ViewController.h 文件做了一个小改动。现在也变回原来的样子了。

从那时起,每当我将任何数字按钮连接到代码并在模拟器中按下它时,我的应用程序就会崩溃。我删除了旧的连接,重新建立了它们。 Received connections 菜单中没有任何感叹号。也尝试了 Refactor 选项以恢复到我原来的工作代码。即使我试图从头开始制作一个新的计算器,但我也遇到了与 libc++abi.dylib 相同的未捕获异常错误:以 NSException 类型的未捕获异常终止。

从早上开始就一直在和它战斗,但没有成功!

如果有人能帮助我解决这个问题,我将不胜感激?

谢谢!

ViewController.h

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextField *calculatordisplay;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.calculatordisplay.text = @"";
}

- (IBAction)calculatordisplay: (UIButton*)buttonPressed {
    NSString *digit;
    digit = [buttonPressed currentTitle];
    NSString *calculatordisplay = [NSString stringWithString: self.calculatordisplay.text];
    self.calculatordisplay.text = [calculatordisplay stringByAppendingString:[buttonPressed currentTitle]];
}

- (IBAction)clear:(UIButton*)buttonPressed {
    self.calculatordisplay.text = @"";
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end

输出:

2015-09-19 20:33:45.808 MyCalculator[8226:339440] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSPlaceholderString initWithString:]: nil argument'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000106e4af65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001068c4deb objc_exception_throw + 48
    2   CoreFoundation                      0x0000000106e4ae9d +[NSException raise:format:] + 205
    3   Foundation                          0x00000001064b5d9d -[NSPlaceholderString initWithString:] + 96
    4   Foundation                          0x000000010646b2f9 +[NSString stringWithString:] + 45
    5   MyCalculator                        0x00000001063c3873 -[ViewController calculatordisplay:] + 195
    6   UIKit                               0x00000001071f91fa -[UIApplication sendAction:to:from:forEvent:] + 92
    7   UIKit                               0x000000010735d504 -[UIControl sendAction:to:forEvent:] + 67
    8   UIKit                               0x000000010735d7d0 -[UIControl _sendActionsForEvents:withEvent:] + 311
    9   UIKit                               0x000000010735c906 -[UIControl touchesEnded:withEvent:] + 601
    10  UIKit                               0x0000000107263aa3 -[UIWindow _sendTouchesForEvent:] + 835
    11  UIKit                               0x0000000107264691 -[UIWindow sendEvent:] + 865
    12  UIKit                               0x0000000107216752 -[UIApplication sendEvent:] + 263
    13  UIKit                               0x00000001071f1fcc _UIApplicationHandleEventQueue + 6693
    14  CoreFoundation                      0x0000000106d770a1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    15  CoreFoundation                      0x0000000106d6cfcc __CFRunLoopDoSources0 + 556
    16  CoreFoundation                      0x0000000106d6c483 __CFRunLoopRun + 867
    17  CoreFoundation                      0x0000000106d6be98 CFRunLoopRunSpecific + 488
    18  GraphicsServices                    0x000000010a547ad2 GSEventRunModal + 161
    19  UIKit                               0x00000001071f7676 UIApplicationMain + 171
    20  MyCalculator                        0x00000001063c3dcf main + 111
    21  libdyld.dylib                       0x00000001094d692d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我会检查您尝试解析的字符串

[NSPlaceholderString initWithString:] + 96

初始化字符串后,看起来 error/exception 恰好在您设置 NSString 时发生。

按钮是否设置了传递的字符串值?换句话说,当您创建“9”按钮与“3”按钮时,您如何区分它然后解析所需的操作?我怀疑你需要检查按钮的标签。