iphone - 计算器程序中的断言失败

标签 iphone ios objective-c macos cocoa

所以我正在开发一个简单的计算器程序来习惯 cocoa 和 Objective C。我已经多次重做了整个事情,每次我完成编码,第一次构建它,它工作得很好,但是每次此后,窗口都不会启动,并给出以下错误:

2013-01-11 10:32:14.760 Visual Caluclator Fix[39892:403] *** Assertion failure in -[NSTextFieldCell _objectValue:forString:errorDescription:], /SourceCache/AppKit/AppKit-1138.47/AppKit.subproj/NSCell.m:1564
2013-01-11 10:32:14.762 Visual Caluclator Fix[39892:403] Ignoring exception raised in __-[NSPersistentUIManager restoreAllPersistentStateWithTalagentWindows:registeringAsReadyWhenDone:completionHandler:]_block_invoke_3: Invalid parameter not satisfying: aString != nil

我得出的结论是问题出在我的textEdited方法上,因为当我注释掉其中的代码时,程序运行没有问题;但是,我不知道为什么会这样,也不知道为什么它会第一次运行而不是随后的任何一次。当我放入异常断点时,它会将我指向 updateUI 方法中的一行以及 textEdited 方法中对 [self updateUI] 的调用。下面的代码是textEdited方法和它引用的其他方法。(我相当确定solve方法没有任何问题,因为我在命令提示符计算器中使用了它并且效果很好。另外,我知道这是一个非常复杂的方法使用字符串和所有内容来编写计算器程序的方法,但我只是想将命令提示符程序已有的代码集成到 cocoa 程序中。)

在 AppDelegate 类中:

- (void)updateUI{
    [self.calculationView setStringValue: self.calculation.calcString];//Exception breakpoint points here
}

- (IBAction)textEdited:(id)sender {
    self.calculation.calcString = self.calculationView.stringValue;
    [self.calculation solve];
    [self updateUI];//Exception breakpoint points here
}

在计算类中:

- (NSString*)solve{
    for (int i = 0; i < [self.calcString length]; i++) {
        NSRange nextChar = NSMakeRange(i, 1);
        if ([[self.calcString substringWithRange: nextChar] isEqualToString: @"*"]||
            [[self.calcString substringWithRange: nextChar] isEqualToString: @"/"])
            [self calcTerm: i];
    }
    for (int i = 0; i < [self.calcString length]; i++) {
        NSRange nextChar = NSMakeRange(i, 1);
        if ([[self.calcString substringWithRange: nextChar] isEqualToString: @"+"]||
            [[self.calcString substringWithRange: nextChar] isEqualToString: @"-"])
            [self calcTerm: i];
    }
    return self.calcString;
}

最佳答案

这可能有助于解决您的问题:

http://www.raywenderlich.com/10505/my-app-crashed-now-what-part-2

该网站解释了断言错误的工作原理,并演示了如何修复此类错误。

关于iphone - 计算器程序中的断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14288085/

相关文章:

css - iPhone 6 的响应式设计?

ios - 如何在 swift/objective c 中打开所有 iphone 7 LED

objective-c - IOS:停止 UIAlert 循环

ios - 试图了解 didselectrowatindexpath 发生了什么。应用程序崩溃,消息索引 1 超出范围 [0 .. 0]'

objective-c - 用于重新排列空间的 API

iphone - 将 UIView 保留在 Circle Objective-C 中

iphone - iPhone 上的 sqlite 在插入/更新时锁定整个数据库还是仅锁定特定表?

javascript - 评估JavaScript在WkWebview上缓慢(非常慢)地绘制数据

iphone - 带有当前时间的 UILabel

iphone - 将 youtube channel 加载到 uitableview