そこで、ココアと客観的 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 メソッド内の 1 行と、textEdited メソッド内の [self updateUI] の呼び出しが示されます。次のコードは、textEdited メソッドとそれが参照するその他のメソッドです。文字列とすべてを使って電卓をプログラムする方法ですが、コマンドプロンプトプログラム用に既に持っていたコードを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;
}